简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM