RoR differences between :url, :action, :method in form_for - ruby-on-rails

There might be answers in documentation, but i don't seem to find good answers.
So among the three :url, :action, :method, what are their differences when used in form_for in Rails?

Difference between :url, :action and :method
:url
If you want to submit your form for any particular controller, any particular action and want to pass some extra parameter (use action that define in controller that you pass on controller )
for example
<%= form_for #post, :url => {:controller => "your-controller-name", :action => "your-action-name"} do |f| %>
In the above code the form is submitted to that controller(that you pass on url) and goto that (you pass on action) action. it will take defaults to the current action.
now suppose you want to pass extra parameter then for example
form_for #post, :url => { :action => :update, :type => #type, :this => #currently_editing } do |f| ...
you can pass extra parameter like :type => #type
so :url is The URL the form is submitted to. It takes the same fields you pass to url_for or link_to. In particular you may pass here a named route directly as well.
:action
form_for #post, :url => { :action => :update, :type => #type, :this => #currently_editing } do |f| ...
In the above example we pass :action if we want to submit the form in different action then we pass :action and your-action-name the form is post to that action
:method
method is used for which method you want to pass for that action. There are several methods like put,post,get ...
for example
form_for #post, :url => post_path(#post), :method => :put, ....
In the above form_for we pass :method => :put when this form is submit it will use put method

form_for is basically used on object. For example:
<% form_for #person do |f| %>
...
<% end %>
When you click submit it will go to default action like from :new to :create, :edit => :update. If you want to specify your own action then you have to use :url and :method is used to force to post or get. For example:
<% form_for #person :url => {:action => "my_action"}, :method => "post" do |f| %>
...
<% end %>

URL:
Url is the path where your form data should go.
whatever you write inside the :url symbol is considered as the path where your data should go when u click a submit button in the form.
Action:
Action is the method in your controller, in your form_for #user (where #user is a object of User model), if you say :action => create then it sumbit the data to users_controller 'create' function (def create).
You will mention this inside :url to tell that the data should go to the specifiled action.
Method:
Is http method, there are 'get', 'post', 'update', 'patch' and 'delete' method. You can learn about this in google.

Related

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

create new object with params from other object

I have in my controller a action with #post = Post.new
and in my view the next form:
<%= form_for(#post, :method => :get, :remote => true, :url => { :controller => "posts", :action => "new_posts_from_web" }) do |f| %>
<%= f.text_field :remote_posted_url%>
<%= f.submit "Find Images" %>
I want create in action new_posts_from_web a new post with params sent by the previous form, and I have this in new_posts_from_web action:
def new_posts_from_web
#post = Post.new(params[:id])
end
but I get a new post without params.
How can I get params from a form in other action without save in database the object?
#post = Post.new(params[:post])
should work fine.

Customize action to be called in a controller from a form in rails 3

I have a model Product that is both used by a general controller products_controller and another one used for administration purposes products_controller in an /admin folder.
Because I wanted to create a form that will be sent via AJAX and depending on whether the product is there or not it will alter between a :create and an :update, I've decided to create a 3rd method that takes care of that; called insert.
def insert
#blah
end
The form tag I'm using for updating is:
<%= form_for(product, :url => admin_product_url(product), :remote => true, :layout => true) do |f| %>
So my question is how can I customize the form tag in order to call the insert action I tried:
<%= form_for(product, :url => admin_product_url(product), :action => :insert, :remote => true, :layout => true) do |f| %>
and
<%= form_for(product, :url => insert_admin_product_url(product), :remote => true, :layout => true) do |f| %>
so far with no luck.
I hope someone could show me the way to go.
Thanks
you should have to add below route in route file under the admin name space
match 'products/insert', :to =>'products#insert', :as => 'admin_products_insert'

Additional Parameter in form_for in 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| %>

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