Hidden field in rails form - ruby-on-rails

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)%>

Related

SimpleForm: SyntaxError in Controller

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| %>

call partial with object from query call

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!

ruby on rails passing an instance variable from view to controller

I have the follwoing form in my view :
I have an instance variable #selected_folder somewhere above in this view
<%= form_for :workflow_selection, :remote => true, :method => "get", :url => {:action => "final_submission"} do |f| %>
<p> Check the workflows needed and then click on the button with folder name</p>
<% #workflow_map[#selected_folder].each do |i| %>
<p><%= f.check_box(i)%> <%= f.label(i, i)%><br /><p>
<% end %>
<br />
<p><%= f.submit #selected_folder%></p>
<% end %>
I want to label the submit button as just 'submit' and should still be able to pass the #selected_folder instance variable to the final_submission action mentioned in the form_for tag
I tried various option like
<%= form_for :workflow_selection, :remote => true, :method => "get", :selected_folder => #selected_folder
:url => {:action => "final_submission"} do |f| %>
i tried to create a select drop down and hide it from view but still trying it to pass once the submit button is clicked.
and some more options..
None of them worked
Please help.
If you want to pass #selected_folder along in the form submission, you can add a hidden_field_tag.
As per Rails documentation:
hidden_field_tag(name, value = nil, options = {})
So in your case
<%= hidden_field_tag 'selected_folder', #selected_folder %>
in the workflow_selection, selected_folder will be present in the form hash.

How to make "form_for" use "remote => true" based on condition?

I have a form that should use "remote => true" when it's updating a record, but not when creating a new one.
I tried:
<%= form_for position position.new_record? ? (, :remote => true do) |p| %>
syntax error...
To fix your example:
<%= form_for position, (position.new_record? ? {} : {:remote => true}) do |p| %>
But to make it a bit nicer you could do this:
<%= form_for position, :remote => position.new_record? do |p| %>

Formtastic set class and id of form

How can I set the class and id attribute for the form element through semantic_form_for?
The following code:
<% semantic_form_for (#meetingsearch), :class => "new_meeting_search", :id => "meeting_search" do |f| %>
gives me:
<form action="/meetingsearches" class="formtastic meetingsearch" id="new_meetingsearch" method="post">
This should do what you need (untested):
<% semantic_form_for #meetingsearch, :html => { :class => "new_meeting_search", :id => "meeting_search" } do |f| %>
For clarification, semantic_form_for wraps around Rails' built in form_for, so this is exactly how you do it in regular Rails forms also:
<% form_for #meetingsearch, :html => { class => "new_meeting_search", :id => "meeting_search" } do |f| %>

Resources