Is the following line in Rails Simple form
<% form_for #user :url => {:action => "attempt_login"}, do |f| %>
The same as the following in Rails form helper?
<%= form_tag(:action => 'attempt_login') do %>
If not, can you tell me what it would be? I need to redo some form code and I would like write down the correct syntax before running the app...
For in the case of passing parameters (i.e. :action) the simple form documentation is rather ambiguous.
Thanks!
The form_for is usually used for a specific record in order to update or create it.
Example:
# view in HAML (not ERB)
= form_for #user do |f|
= f.text_field :username
# matched with the routes
resources :users
other example with nested resources:
# routes.rb
resources :users do
resources :posts
end
# view in HAML
= form_for [#user, #post] do |f|
= f.text_field :content
Since you gave a record as an argument to the form_for method, it can "pre-fill" some fields of your record, i.e. if the #user already has a value for username, the field will be populated with that username.
It is true that you can specify an action to the form_for, something like this:
= form_for #user, url: { action: :custom_action } do |f|
The form_tag is used for the "other forms", such as a login form or a specific controller's action to be done.
Example:
# view in HAML
- form_tag action: :login do
= text_field_tag :username
= password_field_tag :password
To conclude, I would (based on my opinion) use the form_for helper if you are actually using a model's instance in the form and trying to modify/create it. So in your case, I would not use the form_for helper but use the form_tag instead (because you want a login form).
I recently migrated my form_tag methods to form_for because form_for was used in a gem that formatted my form using bootstrap. This was for a login form with no model. Here's how I used form_for without a model:
<%= form_for(:login, :action => 'attempt_login') do %>
I assume this works for simple_form as well:
<%= simple_form_for(:login, :action => 'attempt_login') do %>
Instead of using a form_tag, because the gem didn't decorate it, I used a form_for with a :symbol instead of a #model
Related
In a rails 4 application, I have a book resource, that is a Book model with its controller, views and route. It's what gets created by:
rails g scaffold book title
Now I want to have another set of views (and another controller) that allows to manage the same model, maybe dedicated to a different user.
I want both the creating function and the editing function to be available on this different route and view, .
Let's call it book2.
The views in the /book2 url should operate on the Book2sController.
form_for support
But the form_for guesses the submit route (and puts it in the action attribute) from the model class, that, being it always Book, lets rails guess that the submit url is /books/1 for edit or /books/ for new and not /book2s/1 for edit and /book2s/ for new as it should be.
So i found this solution, but i find it to be a bit cumbersome.
Is there anything better out there?
<%= form_for #book, :url => #book.new_record? ? url_for(book2s_path) : url_for(book2_path(#book)) do |f| %>
<%= f.text_field :title %>
<% end %>
You could set the url in your controller.
def new
# ...
#form_url = book2s_path
# ...
end
def edit
# ...
#form_url = book2_path(#book)
# ...
end
Then your view becomes:
<%= form_for #book, :url => #form_url do |f| %>
<%= f.text_field :title %>
<% end %>
I have also seen:
<%= form_for #book, :url => {:controller => 'book2s', :action => #action} do |f| %>
<%= f.text_field :title %>
<% end %>
and you just set #action in the controller (probably create or update).
Note that you don't need to include the url_for like you have.
resources :users, shallow: true do
resources :shoes
end
This gives me two different routes for create and edit
user_shoes_path
shoes_path
In my shoes _form.html.erb if I leave the form tag :url as default I get a missing routes error when I submit a new or updated shoe.
If I supply the url in the form I can get it to work for either the new or the edit update, but I can't get it to work for both.
This works for the new:
<%= simple_form_for :shoe, url: user_shoes_path do |f| %>
This works for the edit, but will fail once it tries the actual update since it redirects to /:param_id:
<%= simple_form_for :shoe, url: shoes_path(#shoe) do |f| %>
How can I make it work for both? Thanks.
You should use
<% = simple_form_for [#user, #shoe] do |f| %>
and let do simple_form do the work...
This case, if there is a #user, simple form will use it (as for a new), if there isn't (like for an edit), simple form won't use it...
My code is working on local server but it's not working on the production server. I can't figure it out what I'm doing wrong.Please help me.
This is where I'm getting error in my partial:
<%=form_for #shiftchange, :url => { :controller=>"schedulers" ,:action => "shift_change" },:validate=>true , :method => :post do |f|%>
<%= f.label :from_date , "From Date " %>
<%= f.text_field :from_date ,:class =>'datepicker' %>
<% end %>
To load the partial,this is what I'm doing this:-
<%= render "schedule_shift" %>
In the controller I have this:
#shiftchange = Shiftchange.new
If the form is included for multiple actions (pages) you need to set #shiftchange to something or the form_for directive will fail (it won't have anything to create a form for).
A much better approach (and the Rails default) is to have separate views for each action, each including only the elements required by that action, and loaded into your application.html.erb layout with <%= yield %>. So you would have a app/views/shiftchanges/new.html.erb view which has the form in it. You never very rarely need to define any load paths in Rails, they are all derived from the model, controller and action names - or from your routes.rb. This is a core part of the convention over configurationhttp://en.wikipedia.org/wiki/Convention_over_configuration paradigm which runs so deep in Rails.
If you do need to have a form for creating a new object on every page (often used for ShiftchangeSessions for example), you can rewrite your form_for so that it doesn't depend on an object being present:
form_for(Shiftchange.new)
or if you need to force it to post to the create method
form_for(Shiftchange.new, :url => { :action => "create" })
You can read more about resource driven form_for in the Ruby On Rails API docs http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for-label-Resource-oriented+style.
Change
form_for #shiftchange
for this
form_for :shiftchange
i guess it works
I have a model 'Article' and a model 'Ratings' nested within articles.
/articles/123/ratings
I want to change the routing of the f.submit in ratings/_form.html.erb
now it is so, that after pressing submit, my application routs to
/ratings/111
but I want to route it to
/article/123
How can I change the routing in a form_for f.submit button.
I have found here something like this:
<% form_for :thing, :url =>
url_for(:action => "update", :id => #thing) do |f| %>
But this do not work for my rails 3.2. Thanks for your help,
:url - 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. Defaults to the current action.
<% form_for :thing, :url => {:action => "update", :id => #thing} do |f| %>
you can also pass it the path_name by using the helper. so you can also do something like
:url => update_article_path(#article)
Try form_for (:thing, url:{:controller=>'thing', :action=>'update'}, html:{method:'put'}).
I have the following form_for declaration in a Rails site I am building:
form_for(form_question, :url => { :controller => "form_questions", :action => "edit", :id => form_question.id }) do |f|
but the site renders;
<form action="/form_questions/1/edit">
why is it putting the '/1/' before the "edit" in the action url?
Simple Answer. RESTful routes.
Basically you have defined resources :form_questions in config/routes.rb and that is transforming, automagically, your URL to make it RESTful.
I would recommend using the RESTful helpers provided to you, like:
<% form_for(#form_question) do %>
<%= f.text_field :question %>
...
<% end %>
Which will generate a URL to either create or update depending on if the #form_question response to new_record? is true or false respectively. It'll also do other things, like give the form tag a different id attribute based also off what new_record? returns.