ruby on rails passing an instance variable from view to controller - ruby-on-rails

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.

Related

How do you add an extra query string parameter via a form submit in Ruby on Rails?

I would like to be able to add an additional query string parameter on submit that is the same as the value of the classrooms_search_textbox that the user will type. How do I do this?
<%= form_tag classrooms_path, :method => :get, :id => "classrooms_search_form" do %>
<%= text_field_tag "classrooms_search_textbox", "Find a classroom" %>
<%= submit_tag "Find", :id => "classrooms_search_button", :class => "button" %>
Do I need to add a hidden_tag (and if so, how would I go about doing this?) or can I just add to the classrooms_path somehow?
Thanks!
Since you're sending it your controller first, then you can just manipulate the params in your controller method before sending it off:
params[:classrooms_query] = params[:classrooms_search_textbox]
And then go ahead and use those params to send off to the other service. There's no need to add hidden field tags or use some fancy JS code.
<%= form_tag classrooms_path, :method => :get, :id => "classrooms_search_form" do %>
<%= text_field_tag "classrooms_search_textbox", "Find a classroom" %>
<%= hidden_field_tag "classrooms_query" %>
<%= submit_tag "Find", :id => "classrooms_search_button", :class => "button" %>
$('#classrooms_search_form').submit(function() {
$('#classrooms_query').value($(classrooms_search_textbox.value());
});
That would achieve what you want. Nonetheless, maybe it is in your interest to refactor the controller or view so that it doesn't have this kind of conflicts.

Rails : How to use an object along with a text_area_tag of form_tag?

<%= text_area_tag :body_html, "", :class =>"required mceEditor", :id=>"txaEditableContent" %>
This is my code. I want this text_area_tag to be assigned to an object(object is note in my case) just like we use "form_for object" so that the :body_html goes straight into params[:note] on submit. How can I do this?
You have got to set first an instance of Note in the controller corresponding action:
#note = Note.new
Then, in your view put this form:
<%= form_for #note do |f| %>
<%= f.text_area :body_html, :class => "required mceEditor", :id => "txaEditableContent" %>
<%= f.submit "Submit" %>
<% end %>
Now when you click the submit button you should get the right post request as you needed.

Editing a single form field with rails

I have a form field in a view that is separate from my initial form. I want the person using the application to be able to edit a single field without having to pull up the entire form. My code is as follows
<%= form_for :user, :url => {:controller => 'users', :action => 'update' } do |f| %>
<%= f.text_field :barcode %>
<%= submit_tag 'Register' %>
<% end %>
When trying to submit the changes to the specified form field I receive an error on the create method I believe. It redirects me to the user controller with the proper id but gives me the following error.
Unknown action
The action '1' could not be found for UsersController
I have tried changing the method from update to create, but then it brings up the blank form, I just want to be able to edit the specified field without having to re-create the form and get the error. Any ideas?
You are not passing the user object to the form.
Try also using the path helper generated by the routes:
<%= form_for #user, :url => user_path(#user) do |f| %>
<%= form_for(#user), :url => url_for(:controller => 'users', :action => 'update') do |f| %>
<%= f.text_field :barcode %>
<%= f.submit, 'Register' %>
<% end %>
It should work now...

form_for should go to create in the controller but it doesn't

I am new to rails and just wrote a pretty complex for that updates the child elements of a video model I created.
The problem is, when I click the submit buttons it should go to the update function in the controller but instead it does nothing.
Here is my code:
<%= form_for :video, :url => video_path(#video), :html => { :method => 'put' } do |f| %>
.
.
.
<p><%= submit_tag "Update video" %></p>
<% end %>
What am I doing wrong?
:method => 'put' is not :html option so try this:
<%= form_for :video, :url => video_path(#video), :method => 'put' do |f| %>
.
.
.
<p><%= submit_tag "Update video" %></p>
<% end %>
OK, I found out what the problem was. In my routes file the rout for the video path was directing video/:id to a different location. after commenting that line it got to the update function.

Rails - I need a checkbox to change a field in the DB

I know I have done this before, but for the life of me I can't figure it out.
I have a table with a "called" field in it. I need to use a checkbox to update the db and check if "called" is "true" or not. Doesn't need to be AJAX, just needs to update the field.
table: rsvp
field: called
Thanks a ton.
A simple approach without ajax could be using a checkbox inside a form and submitting the form with the checkbox javascript onclick event.
Example:
View:
<% form_for #rsvp, :id => "rsvp" do |f| %>
<%= f.check_box :called, :onclick => "$('#rsvp').submit()" %>
<% end %>
this if you are using JQuery... with prototype the onclick string will be:
$('rsvp').submit()
Controller:
#rsvp = Rsvp.find(params[:id])
if #rsvp.update_attributes(params[:rsvp])
# success
else
# fail
end
Reference:
check box
In view:
<% form_for :rsvp, :url => {:controller => "rsvp", :action => "update"} do |f| %>
<%= f.check_box :called %>
<%= f.submit "Update" %>
<% end %>
In rsvp controller, update method:
#RSVPobject.updateAttribute(:called, params[:rsvp][:called])
If you just want to do this just by clicking the checkbox, you need to go the Ajax road.
Try using an "observe_field" in your view.
<%= observe_field ":called",
:frequency => 0.25,
:update => 'feedback_to_user',
:url => {:action => :mark_as_called},
:with => 'called',
:on => 'click' %>
All the details here.
Don't forget to adjust your routes so that the "mark_as_called" action can be found.

Resources