'Change parameter name FORM_WITH VS FORM_FOR
I am using rails 5 and the form_with
helper. My code works when using Form_for
, but not Form_with
. Is the :as
parameter not valid with Form_with
?
View
<%= form_with(model: @user_wizard , :as => :user_wizard, url: validate_step_wizard_path, local: true, builder: BsFormBuilder) do |f| %>
Model
def user_wizard_params
params.require(:user_wizard).permit( :name )
end
However, every time I try to submit the form, the parameter submits:
{"utf8"=>"✓",
"authenticity_token"=>"LvRDsdfsdfsd3V0l4NLg14q2JWBdwkDPqUIu2l7SXDiioCtvMwW6Bv3ss/LPSS9+bdxiPIzjg==",
"current_step"=>"step1",
"wizard_user_step1"=>
{"name"=>"Name"}
}
I have been following this.. https://medium.com/@nicolasblanco/developing-a-wizard-or-multi-steps-forms-in-rails-d2f3b7c692ce
However, my code I use Form_with and they use Form_for. I don't know what the difference is.
Solution 1:[1]
Rails form_with
replaced the as
option with scope
, which has the same functionality. Your example should look like this:
<%= form_with(model: @wizard , scope: :user_wizard) do |f| %>
Now your inputs should have names like user_wizard[attribute]
.
Solution 2:[2]
form_for using underlying model instance for example here @post as a model
<%= form_for @post do |form| %>
<%= form.text_field :author %>
<%= form.submit “Create” %>
<% end %>
form_tag is form helper but don't have
underlying model and you should use text_field_tag instead of text_field
<%= form_tag “/posts” do %>
<%= text_field_tag “post[author]” %>
<%= submit_tag “Create” %>
<% end %>
form_with will combine form_for and form_tag and later these two will be unify with form_with
here implementation of form_for and form_tag using form_with, as you can see below the difference, if you passing form_with with a model it will work as form_for but if you passing it url then it will work as form_tag
<%= form_with model: @post do |form| %>
<%= form.text_field :author %>
<%= form.submit “Create” %>
<% end %>
<%= form_with url: “/posts” do |form| %>
<%= form.text_field :author %>
<%= form.submit “Create” %>
<% end %>
meanwhile for your problem there is no options as
, you can check from this link for more detail options
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sebastian vom Meer |
Solution 2 | widjajayd |