I've got a problem to get my partial working.
I want to pass an object via a local variable to a partial, but I get a
undefined method `model_name' for NilClass:Class
error all the time. But the variable is passed over because I can call it in the partial with .to_yaml, which gives me all the variables properties.
But when I try to use it in a form_for I get that error.
Maybe it has something to do with my db query. Because when i try to call it with another local variable there is no error. But my query should produce a single object, or am I wrong with that?
Here is my show.html.erb:
<%= #partneroffer = Partneroffer.where(:partner_id => #partner.id, :yearname_id => year.id).first %>
<%= render :partial => "form2", :locals => { :partneroffer => #partneroffer } %>
And here is my partial (_form2.html.erb):
<%= form_for partneroffer , :html => { :class => 'form-horizontal' } do |f| %>
<%= f.label :partnerstatus_id, :class => 'control-label' %>
<%= f.collection_select(:partnerstatus_id, Partnerstatus.all, :id, :name) %>
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
partner_year_terms_path, :class => 'btn' %>
<% end %>
It could be that in your partial, partneroffer is a nil object, so it doesn't have the method "model_name".
You are rendering the view the right way, and you are passing the locals the right way, however, are you sure that Partneroffer.where(:partner_id => #partner.id, :yearname_id => year.id).first is not getting a nil value? Try this code in show.html.erb:
<%- #partneroffer = Partneroffer.where(:partner_id => #partner.id, :yearname_id => year.id).first %>
<%- if #partneroffer %>
<%= render :partial => "form2", :locals => { :partneroffer => #partneroffer } %>
<%- else %>
#partneroffer is nil!!!!
<%- end %>
I've found the solution based on one of the related topics (Render :partial a random object from DB by id, in custom form)! The problem was that I have to call the partial four times. But in only one case there was a record in my query results. Each time there wasn't a record rails threw this error message. I knew there was at least a result in one of the partial calls but wasn't aware that there has to be one in each result. Thx to weexpectedthis!
Related
++ edited to change the question to call partial recursively ++
I am writing this question after a lot of days of frustration about this simple piece of code which isn't working. I have read numerous issues with passing partials on stackoverflow and tried all the solutions I could but it doesn't work.
Calling the partial works for the first loop but from second onwards it bombs with an error
"undefined local variable or method `count' for #<#:0x2f12670>"
Notice here that the error contains a class within a class, so something fishy is happening when the partial is being called recursively.
I have a partial called _form.html.erb which calls another partial _rule_action_fields.html.erb. I need to pass a variable from the form partial to the rule_action_field partial and here is the code:
_form.html.erb: (Please note that the builder object here is being passed from code I have not pasted)
<% #folder_count = 1 %>
<% #rule.rule_actions.each do |ruleaction| %>
<%= f.fields_for :rule_actions, ruleaction do |builder| %>
<%= render( :partial => 'rule_action_fields', :locals => { :f => builder, :count => #folder_count } ) %>
<% end %>
<% #folder_count = #folder_count + 1 %>
<% end %>
_rule_action_fields.html.erb
<p id="folder_fields">
<%= count %>
<label class="form_label">Move the files to </label>
<%= f.select(:folder_id, current_user.folders.collect { |p| [p.name,p.id] }, {:include_blank => true } ) %>
</p>
Unfortunately I cant call this partial with a collection of rule_actions because of some logic which is partially in the calling code and partially in the rule_action_fields partial.
Other syntax I have tried:
<%= render :partial => 'rule_action_fields',:f => builder, :locals => { :count => folder_count } %>
<%= render :partial => 'rule_action_fields', locals => { :count => folder_count } %>
<%= render 'rule_action_fields', :count => folder_count %>
Any ideas?
I have this form in a view in my project. I need to pass the task_id to a certain controller, but the log does not seem to be receiving the parameters. I don't know what the problem is.
<%= form_for :taskid, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>
<%f.hidden_field :task_id, :value => task.id%>
<td><%= f.submit "اختر مهمة لاظهار احصائياتها منفرده"%></td>
<% end %>
You are missing on = after <%. The equal sign is needed whenever you want to the result appears on the HTML, so it is used with the field tags methods or render, for instance. You should not use the equal when using a if, for example, because this is not what you want to print (well, it can be, but most likely it isn't)
<%= form_for :taskid, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>
<%= f.hidden_field :task_id, :value => task.id%>
<td><%= f.submit "اختر مهمة لاظهار احصائياتها منفرده"%></td>
<% end %>
However, as #AntonGrigoriev pointed out, you should use a object if you have, like this
<%= form_for #task, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>
or you can simply use the hidden_field_tag
<%= hidden_field_tag :task_id, task.id %>
Hi please test with following code to send hidden value in rails, I have tried and worked for one of my application :
hidden_field_tag(name, value = nil, options = {}) public
eg:
<%= hidden_field_tag(:field_name,value=#offer_status)%>
I've got a Rails app that has two main views: an overview and a sequenced view. The user can enter data in either view. I use the same form helper for both views, but I want the redirect_to from the create action to respect the user's context. So I pass a hidden_field_tag called 'track'.
I can't seem to access the value of 'track' inside my controller.
Here's the form:
<%= form_for([#mission, #mission.stickies.build]) do |f| %>
<div class="field">
<%= f.text_field :name, :size => 60 %>
<%= f.hidden_field :kind, :value => kind %>
<%= hidden_field_tag :track, :value => track %>
<class="actions">
<br><%= f.submit "Add to " + kind.pluralize.capitalize %>
</div>
<% end %>
And here's where I call it in one of the views:
<%= render :partial => "stickies/form" , :locals => { :kind => "driver", :track => 'main' } %>
Here's the parameters dump (from a different call):
{"utf8"=>"✓",
"authenticity_token"=>"N3IXwNQosOfxw1ZcpfFPOLPKzHbvNyaBhAiP3ftT9GY=",
"sticky"=>{"name"=>"Tesssksjd argghh.",
"kind"=>"success"},
"track"=>"{:value=>\"sequence\"}",
"commit"=>"Add to Successes",
"mission_id"=>"32"}
And here's the relevant code in my create controller:
if params[:track][:value] == "main" then
redirect_to mission_path(#mission) + '#' + #sticky.kind.pluralize
elsif params[:track][:value] == "sequence" then
redirect_to mission_stickies_path(#mission, :kind => #sticky.kind)
end
I can't seem to find the syntax, or comparator, or whatever I need to access the value represented by "track"=>"{:value=>\"sequence\"}".
Any help or pointers would be greatly appreciated. I'm new to Rails and Ruby, this is my first app.
Don't write it with the :value => track, rather do:
<%= hidden_field_tag :track, track %>
and access it with params[:track]
I'm trying to observe a field that get generated inside a fields_for loop.
The thing is that the id of that field is generated dynamically.
_form.html.erb
<% form_for #exp, :url => {:action => "update"} do |f| %>
<% f.fields_for:patterns do |builder| %>
<%= render 'pattern_fields', :f => builder %>
<% end %>
<% end %>
_pattern_fields.html.erb
Pattern: <%= f.select(:LC_PATTERN, [['stripes', 'stripes'],
['dots', 'dots'],
['lines', 'lines'],
],{ :prompt => "Please select"}
) %>
<%= observe_field("------", :frequency => 1,
:with => "'id='+value", :function => 'alert(value)')%>
My question is how do i get the id of the field inside the fields_for tag.
I finally got it. Found this solution on the internet...Hope it might be of help to someone else.
In your application_helper.rb, add the following functions:
def sanitized_object_name(object_name)
object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/,"_").sub(/_$/,"")
end
def sanitized_method_name(method_name)
method_name.sub(/\?$/, "")
end
def form_tag_id(object_name, method_name)
"#{sanitized_object_name(object_name.to_s)}_#{sanitized_method_name(method_name.to_s)}"
end
You can then view the id of the fields generated inside 'fields_for' by using the following code:
<%=form_tag_id(f.object_name, :LC_PATTERN) %>
I know some will think that i should use form_tag with :remote=>true, but i don't know how to render an entire html output....
My problem is the following:
I have this form that sends a collection through 3 comboxes
<%= form_remote_tag :url => report_client_reports_path, :update => :graphic do%>
<%#= form_tag reporte_client_reports_path%>
<p><%= label_tag :supermercados %>
<%=select_tag "supermercados[]", options_from_collection_for_select(#supermercados, "id", "name"),{:multiple=>true, :id => "supermarkets"}%>
</p>
<p><%= label_tag :cortes %>
<%=select_tag "cortes[]", options_from_collection_for_select(#cortes, "corte_real","cuts"),{:multiple=>true, :id => "cortes"}%>
</p>
<p><%= label_tag :productos %>
<%= select_tag "productos[]", options_from_collection_for_select(#productos, "id", "name"),{:multiple=>true, :id => "products"}%>
</p>
<p><%= submit_tag 'Send' %></p>
<%end%>
If i uncommented this line:
<%#= form_tag reporte_client_reports_path%>
It work good and present me the graph, but not the way i expect to work.
I have detected that using form_remote_tag, it sends all vars with their values, bu i dont know why my controller only see one value of each variable.
here is the controller:
#super = params[:supermarkets]
#superm = []
#super.each do |s|
#superm << Company.find(s).abbr
end
#cuts = params[:cuts]
#prods = params[:products]
#cortesGraph = []
#cortess.to_a.each do |c|
#cortesGraph << "#{RawData.find_by_real_cut(c).cuts}"
end
The objects #superm,#cuts and #products arent receving more than 1 value in the array, is a routing problem or a option i have missed in the form_remote_tag?
And update to simplyfy, what actually is still happening is this:
This Works:
<%= form_tag reporte_client_reports_path do%>
This doesn't:
<%= form_tag reporte_client_reports_path,:remote=>true do%>
The problem is that :remote is not sending my params as array it send all data as 1 var :s
I resolved in functional way, but there might be something very weird with this in my controller:
if request.xhr?
#super = params[:supermercados].to_s.split(",")
#cortess = params[:cortes].to_s.split(",")
#prods = params[:productos].to_s.split(",")
end