Change parameter name FORM_WITH VS FORM_FOR - ruby-on-rails

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.

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].

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

Related

Why does submit work with form_tag and not with form_for?

I have a form_tag form associated with a gem pg_search that works with a form_tag as below without ajax for the moment in pure html, yet when I try to make it work the same way with a form_for the submit does not pass.
Do you know why?
In passing do you know the equivalent in form_tag of the helper "url: search_path" which is commented in the form_for?
Thank you
1. Le form_tag
<%= form_tag new_product_association_path, method: :get do %>
<%= text_field_tag :query, params[:query], id: "text_field", class: "form-control", placeholder: "Find a product" %>
<%= submit_tag "Search", class: "btn btn-primary", id: "submit-search"%>
<% end %>
2. le form_for
<%= form_for product_associations_url, as: :get do |f| %>
<%= f.text_field params[:query], id: "text_field" %>
<%= f.submit "Search", class: "btn btn-primary", id: "submit-search" %>
<% end %>
Rails provides several ways to create HTML for a form.
form_tag: This generates the HTML in most raw form. You specify various attributes and resulting form is generated with no Rails magic.
form_for: This is Rails way of creating forms associated with a resource, e.g.: a product. You have to pass in the object which you want to create / update. So, the way you are writing form_for is incorrect. One more issue (typo) is instead of using as to specify method, you should use method.
Basic Usage of form_for:
In Controller:
#product = Product.new
In Views:
<%= form_for #product do |f| %>
...
<% end %>
The issue might be that the form_for helper expects a record as its first argument, not an URL. I suggest switching to form_with which also allows for URL usage.
Furthermore the text_field usage also isn't valid. You should still provide the name (:query) of the input field.
<%= form_with url: product_associations_url, as: :get do |f| %>
<%= f.text_field :query, value: params[:query], id: "text_field" %>
...
<% end %>
Example usage can also be found all over the Action View Form Helpers guide.

Rails custom text on submit button using variable from partial

I have the following form inside a partial named _configuration_form.html.erb
<%= form_for #configuration, url: admin_config_path, method: :put do |f| %>
<%= f.label :chars %>
<%= f.number_field :chars %>
<%= f.submit #submit_text %>
<% end %>
where I'm passing in #configuration and #submit_text as local variables into the partial, as follows:
<%= render :partial => 'layouts/admin/configuration_form',
:locals => {#configuration => :configuration, #submit_text => "Update configurations"} %>
The #configuration variable appears to be working properly as it is drawing the data from the controller. However, the submit button is showing the default text, as per rails defaults.
My question is: am I doing something wrong here to prevent the desired custom button text from appearing, or is there a way to enforce the use of a local variable? I also tried using "#{#submit_text}" which just showed an empty button (no text at all). Any advice welcome.
The proper syntax for passing locals into a partial is:
<%= render :partial => 'layouts/admin/configuration_form',
:locals => {configuration: #configuration, submit_text: "Update configurations"} %>
Then, the partial should be:
<%= form_for configuration, url: admin_config_path, method: :put do |f| %>
<%= f.label :chars %>
<%= f.number_field :chars %>
<%= f.submit submit_text %>
<% end %>
Alternatively, you could instantiation the #submit_text in your controller and leave your partial like:
<%= form_for #configuration, url: admin_config_path, method: :put do |f| %>
<%= f.label :chars %>
<%= f.number_field :chars %>
<%= f.submit #submit_text %>
<% end %>
And in your controller and call your partial like:
<%= render :partial => 'layouts/admin/configuration_form' %>
In this case, the partial will use the controller variables rather than locals.

Rails form_for syntax in parent child association

I'm trying to wrap my head around the form_for syntax, when using parent/child associations. Basically my question is, what are the differences in the following, as I've seen most of them being used:
<%= form_for (Child.parent) do |f| %>
<%= form_for (#child.parent) do |f| %>
<%= form_for (#child, #parent) do |f| %>
<%= form_for (#child, #parent.child) do |f| %>
<%= form_for (Child, #parent) do |f| %>
<%= form_for ([Child.parent]) do |f| %>
<%= form_for ([#parent.child]) do |f| %>
I'd say it depends on what you're trying to accomplish. I've used this before:
<%= form_for [#parent, #child] do |f| %>
or even with a namespace:
<%= form_for [:namespace, #child] do |f| %>
If you're looking for nested fields perhaps this would be more what you're looking for:
<%= form_for #parent do |f| %>
...
<%= f.fields_for :children do |builder| %>
<%= render "child_fields", f: builder %>
<% end %>
...
<% end %>

Ruby Rails is it possible to check before displaying form?

Is it possible to check submission before showing this simple form:
<%= form_for #article, url: {action: "create"}, html: {class: "nifty_form"} do |f| %>
<%= f.text_field :title %>
<%= f.text_area :body, size: "60x12" %>
<%= f.submit "Create" %>
<% end %>
like if its already submitted first time show other html content instead ?
Test the object. If you're using ActiveRecord/Mongoid:
<% if #article.new_record? %>
... display form ...
<% else %>
... do something different ...
<% end %>

correct syntax for form_for url in rails

This is probably simple, I'm still coming to terms with rails syntax. What is the right syntax to pass the address_id in the url for form_for to a modified route?
This is the form - note the "address_id parameter"
<div class="one_fourth floatcenter">
<%= form_for address, :url => edit_address_path(:id => address.id), :method => :get do |f| %>
<%= content_tag(:button, :class => 'btn btn-inverse') do %> Edit Address
<% end %>
<% end %>
</div>
And this is the route I've configured:
get "edit_address/:id" => "member/addresses#edit"
Id is not being passed to the controller for some reason...
form_for address should be enough if address is a persisted object, but if it's not enough, then form_for address, url: edit_address_path(address) is what you want.
This is very simple. In place of url, you put your post method route:
<%= form_for(#post, url: super_posts_path) do |f| %>
...
<% end %>
You also call by action
<%= form_for #friend,:url=> { action: "create_friend"} do |f|%><br>
<%= f.label :u_from %>
<%= f.text_field :u_from %>
<%= f.label :u_to %>
<%= f.text_field :u_to %>
<%= f.submit%>
<% end %>

Resources