Additional Parameter in form_for in Rails - ruby-on-rails

Is it possible to submit another parameter outside the form data in rails? My problem is that I render different forms for different classes and send them to the same create method. I would like to send the class with the form (as value not as key in the hash).
Something like the :type parameter (that actually doesn't work)
<%= form_for(#an_object, :url => { :controller => :a_controller, :action => :create },
:type => #an_object.class.to_s.underscore) do |f| %>
The post message looks like:
{"commit"=>"Create Class of an Object",
"authenticity_token"=>"/iqu0A8/AocDT3HyjL5/+bKZiLkyr4FE71u/mc8Wx0Y=",
"utf8"=>"✓",
"class_of_an_object"=>{"name"=>"a name",
"description"=>"a description"}}
and I would have a "type" => "class_of_an_object", but directly in the hash an not within the "class_of_an_object" hash.

<%= form_for #an_object,
:url => { :controller => :a_controller,
:action => :create,
:type => #an_object.class.to_s.underscore } do |f| %>
And I prefer to use named routes
<%= form_for #object, :url => object_path(#object, :type => "whtever"), :html => {:method => :post} do |f| %>

This works for me:
<%= form_for #foo, :url => foo_path(:type => "whatever"), :html => {:method => :post} do |f| %>

Related

How to pass value in controller action in rails 5?

I am redirecting a user to a custom controller action like this:
<%= link_to url_for(:controller => :jobs, :action => :saved) do %>Saved Jobs<% end %>
it works fine, now i want to pass some value to action "saved".
Do you have any idea how to do this?
Try This
If you want to send the form directly by reloading the page as you are doing,
<%= link_to url_for(:controller => :jobs, :action => :saved, method: :post, :email => "value") do %>Saved Jobs<% end %>
If you want to send it through ajax call from background, add :remote => true to the form,
<%= link_to url_for(:controller => :jobs, :action => :saved, method: :post, :email => "value", :remote => true) do %>Saved Jobs<% end %>
Then in controller
#email = params[:email]

How do I update create route from rails 3 to 4

I have a form tag which is seen below, when I try to run the page I get the error: No route matches {:action=>"create", :type=>"new", :controller=>"lists"}
<%= form_for #list, :url => {:action => "create", :type => "new"}, :html => {:multipart => true,:role=>"form"} do |f| %>
In my routes file i have a line-- resources :lists
I thought the line above in the routes file is supposed to create correct routes for me.
Can someone tell me what I am doing wrong?
The url option needs a controller if you're going to format things this way... and there isn't a type option in url_for... so it should probably look like this:
<%= form_for #list, :url => {:controller => 'lists', :action => "create"}, :html => {:multipart => true,:role=>"form"} do |f| %>
Or without the hash rockets:
<%= form_for #list, url: { controller: 'lists', action: "create" }, html: { multipart: true, role: "form"} do |f| %>
check the method, create must be sent via POST as
<%= form_for #list, as: :list :url => lists_path, method: :post do |f| %>

How to send link_to parameters using POST on Rails 2

I have a link_to like this:
<%= link_to "Nuevo Contrato", {:controller => "hotels", :action => "edit", :id => #hotel_id, :selected_tab => "4"}, :class => "new_link" %>
There's a way to send those parameters not using the query string for it? (using post instead of a get) ??
Thank you!
I already tried:
<%= link_to "Nuevo Contrato", {:controller => "hotels", :action => "edit", :id => #hotel_id, :selected_tab => "4"}, {:method => :post,:class => "new_link"} %>
And it keeps doing the same thing...!
Add a supported HTTP verb in the method option.
<%= link_to "link", {:method => :post ...} %>
I think you can just add :method => :post to the options of link_to.
Here are the docs (for Rails 3, but I think this still holds true for Rails 2 also) http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
EDIT FOR UPDATED QUESTION
:method => :post belongs in the HTML options, not the URL options. This is not very obvious from the documentation.
<%= link_to "Nuevo Contrato", {:controller => "hotels", :action => "edit", :id => #hotel_id, :selected_tab => "4"}, {:method => :post, :class => "new_link"} %>

Rails 3 Rspec form_for Deprecation Warning

I have a form_for in my application which looks like:
<%= form_for :user, #user, :url => { :controller => 'users', :action => 'update' }, :html => { :multipart => true, :method => 'put' } do |f| %>
This works fine. However, when I run my tests with Rspec I always get:
DEPRECATION WARNING: Using form_for(:name, #resource) is deprecated. Please use form_for(#resource, :as => :name) instead.
Which seems to go against what is written in the Rails 3 forms guide. Is this just a bug in rspec or is it actually a deprecation?
You should just be able to do this
<%= form_for #user, :html => {:multipart => true} %>
Since Rails will know that #user is an existing record, it'll know to do a PUT request to users_controller#update.

Rails: Passing multiple parameters to form_for url?

This works great:
- form_for #user, :url => { :action => :create, :type => #type } do |f| ...
Returns /users/(id)?type=type
But in another view I need to pass TWO parameters into the URL string, and this does not work:
- form_for #user, :url => { :action => :update, :type => #type, :this => #currently_editing } do |f| ...
Returns /users/(id)?this=currently_editing
I've also tried:
- form_for #user, :url => { :action => :update, :params = params.merge({:this => #currently_editing, :type = #type})} do |f| ...
... with no luck (error: only GET requests allowed).
What I want is for it to return this: /users/(id)?this=currently_editing&type=type
Thoughts?
Why do you need to pass them into the URL string? Why not just add them as hidden fields in the form? In almost all cases you should pass the variables that way with POSTs.
I would use hidden fields, but this should work:
<% form_for #user, :url => user_path(#user.id, :type => #type, :this => #currently_editing), :method => :put do |f| -%>
:method => :put triggers the update action when using RESTful routes.
please try to this
you can pass more than one parameter in this way.
- form_for #user, :url => xxx_yyy_path(:param1 => value1, :params2 => value2, ......) do |f| ...
I think you have to move the desired querystring attributes outside of the :url option like this:
form_for #user, :url => { :action => :update }, :type => #type, :this => #currently_editing do |f|

Resources