if I want to move the following code into helper
%td.center= log.streaming_verification_id
%td.center= log.id
and render it by calling call_the_helper
How to write the method call_the_helper to meet my requirement
into helper
- #tool_cvt_streaming_verification_logs.each do |log|
%tr
= call_the_helper
Simply to do it by moving the code into an other view (partial), and render it from helper or another view.
view:
- #tool_cvt_streaming_verification_logs.each do |log|
%tr
= call_the_helper(log)
helper:
def call_the_helper(log)
render partial: 'partial_view', locals: { :log => log }
end
partial view:
%td.center= log.streaming_verification_id
%td.center= log.id
Put something like it in your def call_the_helper:
haml_tag :td, :class => 'center' do
log.streaming_verification_id
end
haml_tag :td, :class => 'center' do
log.id
end
Related
I'm looking to extract all records to a single pdf using Prawn PDF and rails 4.2.
Currently I have them generating by id for individual pdf's and works well.
Show
def show
#agreement = Agreement.find(params[:id])
respond_to do |format|
format.html
format.pdf do
pdf = AgreementPdf.new(#agreement)
send_data pdf.render, filename: "Agreement - #{#agreement.entity}", type: "application/pdf", disposition: "inline"
end
end
end
Index in table
<% #agreements.each do |agreement| %>
<tr>
<td><%= agreement.entity %></td>
<td><%= link_to 'Download', agreement_path(agreement.id, format: 'pdf'), :target => "_blank" %></td>
</tr>
<% end %>
First, you have to add a route with the path of the new method:
get '/agreements/all_records' => 'agreements#all_records', :as => :agreement_all_records
Then call the methods with in the view:
<td><%= link_to 'Download All Records', agreement_all_records_path(), :target => "_blank" %></td>
It will look something like this in the controllers:
def all_records
#agreements = Agreement.all
pdf = AgreementsPdf.new(#agreements)
send_data pdf.render,filename:'agreements.pdf',type:'application/pdf', disposition: 'inline'
end
And the Report may look like this ( assuming that agreement model has id,name fields ):
require 'prawn/table'
class AgreementsPdf < PdfReport
TABLE_WIDTHS = [100, 100]
PAGE_MARGIN = [40, 40, 40, 40]
def initialize(agreements=[])
super(:page_size => "LEGAL", margin: PAGE_MARGIN, :page_layout => :landscape)
#agreements = agreements
display_table
end
private
def display_table
if table_data.empty?
text "None data"
else
table(table_data,column_widths: TABLE_WIDTHS, :cell_style => { size: 10 } )
end
end
def table_data
#table_data ||= #agreements.map { |e| [e.id, e.name] }
end
end
Hope this will give you an idea
I'm using an approach from Railscast to remotely access a table for dataTables.
This is the start of the code in workorders4_datatable.rb:
class Workorders4Datatable
delegate :params, :h, :link_to, :number_to_currency, to: :#view
def initialize(view)
#view = view
end
def as_json(options = {})
{
sEcho: params[:sEcho].to_i,
iTotalRecords: Workorder.external.count,
iTotalDisplayRecords: workorders.external.total_entries,
aaData: data
}
end
private
def data
workorders.external.map do |workorder|
[
link_to(workorder.id, workorder),
h(workorder.parent.try(:id)),
h(workorder.description),
h(workorder.client.try(:client_name)),
h(workorder.woasset.try(:assetnum)),
h(workorder.type.try(:typecode)),
h(workorder.billmethod.try(:method_name)),
h(workorder.workgroup.try(:group_name)),
h(workorder.employee.try(:employee_full_name)),
h(workorder.wostatus.try(:statuscode)),
h(workorder.expenses.tobill.sum("quantity") * workorder.expenses.sum("unitcost")),
h(workorder.events.tobill.sum("hours")),
h(workorder.events.sum("hours")),
link_to('ADD Invoice', new_invoice_path(:workorder_id => workorder), :class => 'btn btn-primary')
]
end
end
All of it works except for this line:
link_to('ADD Invoice', new_invoice_path(:workorder_id => workorder), :class => 'btn btn-primary')
It causes this error:
NoMethodError - undefined method `new_invoice_path' for #<Workorders4Datatable:0x007f94513588a0>:
Yet, the following works in a different view:
<%= link_to 'ADD Invoice', new_invoice_path(:workorder_id => #workorder.id), :class => 'btn btn-primary' %>
Is there some way I can get it to work?
Have you tried to replace new_invoice_path with #view.new_invoice_path? That might work. The reason is because new_invoice_path should be called in context of the view, but it's being called in context of Workorders4Datatable.
I've seen this pattern in the past as a shortcut:
def v
#view
end
v.new_invoice_path
I have this link in Rails:
<%= link_to "Add to Journal", add_post_journal_path(post), :method => :put %>
However I want transform this link to show a fancybox with the content listing my content to choose. First, I use this code:
<%= link_to "fancy", "#add_post", :class=>"fancybox" %>
but I have errors, because I want pass the actual post to fancybox, so I'm using this code: in add_post.html.erb:
<h1>Escolha o Jornal que deseja adicionar:</h1>
<ul>
<% current_user.journals.each do |journal| %>
<li><%= link_to journal.name,add_post_complete_journal_path(journal),:remote=>true %> </li>
<% end %>
</ul>
and my controller is:
def add_post
#journal_post = JournalsPosts.new
session[:post_add] = params[:id]
end
def add_post_complete
#journal_post = JournalsPosts.create(:post_id => session[:post_add],:journal_id => params[:id])
respond_with #journal_post
end
How can I transform this code to use my content in my fancybox?
Add on your action add_post the next respond with js:
def add_post
#journal_post = JournalsPosts.new
session[:post_add] = params[:id]
respond_to do |format|
format.js
end
end
Add on a file on your views add_post.js.erb with the next content:
$.fancybox('<%= escape_javascript(render(:partial => 'path_to/add_post'))%>',
{
openEffect: "fade",
closeEffect: "fade",
autoSize: true,
minWidth: 480,
scrolling: 'auto',
});
For example, you have add a partial _add_post.html.erb on your views. Now inside this partial you can write your code view:
#code for your view inside partial `add_post.html.erb`
<%= #journal_post %>
<h1>Escolha o Jornal que deseja adicionar:</h1>
<ul>
.
.
Regards!
I have nested routes like this :
map.resources :foo do |foo|
foo.resources :bar do |bar|
bar.resources :baz
end
end
i have list with pagination in the index action for each resource, i need to caches each of this pages, to do so i need the routes to be RESTful, how do i implements REFTful routes for it?
for example i want the route will be like this :
http://www.example.com/foo/:id/pages/:page_number
http://www.example.com/foo/:id/bar/:id/pages/:page_number
create custom_link_renderer.rb in app/helpers/
class CustomLinkRenderer < WillPaginate::LinkRenderer
def page_link(page, text, attributes = {})
#template.link_to text, "#{#template.url_for(#url_params)}/pages/#{page}", attributes
end
end
add this line to config/environment.rb
WillPaginate::ViewHelpers.pagination_options[:renderer] = 'CustomLinkRenderer'
I had the same problem. I wrote my own LinkRenderer like this to fully use nested routes.
class PaginationListLinkRenderer < WillPaginate::ViewHelpers::LinkRenderer
protected
def page_number(page)
unless page == current_page
if !#options[:params][:url].to_s.empty?
tag(:li, link(page, #options[:params][:url] + "?page=" + page.to_s))
else
tag(:li, link(page, page, :rel => rel_value(page)))
end
else
tag(:li, page, :class => "current")
end
end
def previous_or_next_page(page, text, classname)
if page
if !#options[:params][:url].to_s.empty?
tag(:li, link(text, #options[:params][:url] + "?page=" + page.to_s, :class => classname))
else
tag(:li, link(text, page, :rel => rel_value(page), :class => classname))
end
#tag(:li, link(text, page), :class => classname)
else
tag(:li, text, :class => classname + ' disabled')
end
end
def html_container(html)
tag(:ul, html, container_attributes)
end
end
Then you have to call will_paginate with this parameters:
<%= will_paginate :params => { :url => project_task_lists_path(#project) }, :renderer => PaginationListLinkRenderer %>
I hope this helps :)
beside the fact that accessibility standards discourage the use
of a link pointing to the current page, how I am supposed to
refactor the following view code?
#navigation
%ul.tabbed
- if current_page?(new_profile_path)
%li{:class => "current_page_item"}
= link_to t("new_profile"), new_profile_path
- else
%li
= link_to t("new_profile"), new_profile_path
- if current_page?(profiles_path)
%li{:class => "current_page_item"}
= link_to t("profiles"), profiles_path
- else
%li
= link_to t("profiles"), profiles_path
...
Thank you.
# helpers
def current_page_class(page)
return :class => "current_page_item" if current_page?(page)
return {}
end
-# Haml
#navigation
%ul.tabbed
%li{current_page_class(new_profile_path)}
= link_to t("new_profile"), new_profile_path
%li{current_page_class(profiles_path)}
= link_to t("profiles"), profiles_path
...
#navigation
%ul.tabbed
%li{:class => current_page?(new_profile_path) ? "current_page_item" :nil }
= link_to t("new_profile"), new_profile_path
%li{:class => current_page?(profiles_path) ? "current_page_item" :nil }
= link_to t("profiles"), profiles_path
...
Looks like a good case for a partial to me.