I have a partial called _avatar.html.erb
I want to pass in an id as a local variable called entity_id which will be the id of an object.
<% form_tag({:controller => "avatar", :action => "upload", :id => entity_id},
:multipart => true ) do %>
<fieldset>
<legend><%= title %></legend>
<% if avatar.exists? %>
<%= avatar_tag(avatar) %>
[<%= link_to "delete", {:controller => "avatar",:action => "delete",
:id => entity_id},:confirm => "Are you sure" %>]
...
Here is the call for the parital:
<%= render :partial => 'avatar/avatar', :locals => {:avatar => #avatar,
:title => #title, :entity_id => #board.id } %>
When I try this I get the following errors:
undefined local variable or method `entity_id' for #<ActionView::Base:0x2736bb0>
When I take that out I also get an error telling me it can't find the local variable "title".
Can anyone help this seems to be the correct way to do this.
Thanks in advance
Try using :id=>#entity_id (note the # symbol preceding the name). Likewise, #title, instead of "title".
Related
Any idea on what could be wrong in this form?
Error:
vehicleTrack.html.erb:141: syntax error, unexpected keyword_do_block, expecting => ... params[:rangefrom_string]} do |f| #output_buffer.safe_appe... ... ^
This is my view:
<%= simple_form_for '', url: convertTrackToArea_path, :method => :post,
{ :controller => "vehicles",
:action => "convertTrackToArea",
:search => params[:search],
:rangefrom_string => params[:rangefrom_string]} do |f| %>
<%= f.input :areano, :label => 'Areano' %>
<%= f.button :submit, value: "Crear",:name => nil%>
<% end %>
I have not used simple_form so my guess is you are passing the last last argument incorrectly and it wont take a hash as argument and it detects the argument as key and searching for a value so throws error that => is missing, which is used to identify value in hash. So you can do something like this I suppose:
<%= simple_form_for '',
:url => url_for(:action => 'convertTrackToArea', :controller => 'vehicles',:search => params[:search],
:rangefrom_string => params[:rangefrom_string]),
:method => 'post' do |f| %>
since you are specifying controller and action you dont need to mention convertTrackToArea_path . If that route is already setup, you can just use that like in your posted question and remove controller and action name like:
<%= simple_form_for '', :url => convertTrackToArea_path(:search => params[:search],:rangefrom_string => params[:rangefrom_string]),:method => 'post' do |f| %>
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 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!
I want to pass variable with link_to_remote in Rails 2.3. Following is my code for passing variable. But controller did not get that variable. Anybody can help me ?
<%= link_to_remote 'Add new event', :url => {:controller => 'events', :action => 'new' }, :with=> 'event' %><br>
Any hash key you add that's not part of the keyword set (controller, action, format) will be appended to your URL as an argument.
ie.
<%= link_to_remote "Add New Event", :url => {:controller => 'events', :action => 'new', :var => 'event'} %>
Would yield
/events/new?var=event
Hope this would solve your problem,I have not tried.
<%= link_to_remote "Add New event",
:url => :action => "list",
:with => " 'name=' +$('div-id-of-name-text-box').value + '&city=' +$('div-id-of-city-text-box').value + '&country=' +$('div-id-of-country-text-box').value " %>
I have a partial called _avatar.html.erb
I want to pass in an id as a local variable called entity_id which will be the id of an object.
<% form_tag({:controller => "avatar", :action => "upload", :id => entity_id}, :multipart => true ) do %>
<fieldset>
<legend><%= title %></legend>
<% if avatar.exists? %>
<%= avatar_tag(avatar) %>
[<%= link_to "delete", {:controller => "avatar",:action => "delete", :id => entity_id},:confirm => "Are you sure" %>]
...
Here is the call for the parital:
<%= render :partial => 'avatar/avatar', :locals => {:avatar => #avatar, :title => #title, :entity_id => #board.id } %>
When I try this I get the following errors:
undefined local variable or method `entity_id' for #
When I take that out I also get an error telling me it can't find the local variable "title".
Can anyone help this seems to be the correct way to do this.
Thanks in advance
Are you sure the error is coming from the partial? You are using entity_id in the form_tag. Where is it being defined? title isn't used in the partial. It is used inside of legend though. Is that defined?