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.
Related
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
I have information that is passed to a controller method but isn't saved to the DB. I want to access this information, that is passed to the controller method as a whole hash, but it is all individual data as you will see below.
Here is the params:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"0O7pbNNrddHCyPL9B/avUUD85574rFBfS57h+aWKK/mBakPSn5iHJKHhPmvuJVfyWxjBsAQn2kagwkTOALEKRg==", "page"=>{"content_top"=>"", "content_bottom"=>""}, "header1"=>"iijijij", "column1"=>"ijijijij", "header2"=>"", "column2"=>"", "header3"=>"", "column3"=>"", "header4"=>"", "column4"=>"", "commit"=>"Save", "guide_id"=>"dungeon-boss", "category_id"=>"heroes", "id"=>"link-skill"}
As you can see there is a page hash and after, it is header1 column1 header2 column2... and so on. With the header1 info, I'm trying to put it inside a params hash like the page hash has for the values passed in it. So it's like "table" =>{"header1"=>"iijijij", "column1"=>"ijijijij", "header2"=>"", "column2"=>"", "header3"=>"", "column3"=>"", "header4"=>"", "column4"=>""}
I'm sure there is something I need to add to the form so it know to group them like this. Here is the form I currently have
<% if (current_user.mod_of_game?(#guide) unless current_user.nil?) %>
<%= form_for([#category, #page], url: update_pages_path) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :content_top, "Top Content" %>
<%= f.text_area :content_top, :class => 'editor' %>
<%= f.label :content_bottom, "Bottom Content" %>
<%= f.text_area :content_bottom, :class => 'editor' %>
<!-- to be in one hash when passed -->
<%= text_field_tag :header1 %>
<%= text_field_tag :column1 %>
<%= text_field_tag :header2 %>
<%= text_field_tag :column2 %>
<%= text_field_tag :header3 %>
<%= text_field_tag :column3 %>
<%= text_field_tag :header4 %>
<%= text_field_tag :column4 %>
<!-- end -->
<%= f.submit "Save" %>
<% end %>
I cant find what I need to add to make the text_field_tag data all be in one hash when passed.(the text_field_tag is purposely not being saved to the DB on form submit, it just needs to be passed to the method and grouped inside a hash)
how about using fields_for like this
<%= form_for([#category, #page], url: update_pages_path) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :content_top, "Top Content" %>
<%= f.text_area :content_top, :class => 'editor' %>
<%= f.label :content_bottom, "Bottom Content" %>
<%= f.text_area :content_bottom, :class => 'editor' %>
<!-- to be in one hash when passed -->
<%= f.fields_for :table do |t| %>
<%= t.text_field_tag :header1 %>
<%= t.text_field_tag :column1 %>
<%= t.text_field_tag :header2 %>
<%= t.text_field_tag :column2 %>
<%= t.text_field_tag :header3 %>
<%= t.text_field_tag :column3 %>
<%= t.text_field_tag :header4 %>
<%= t.text_field_tag :column4 %>
<% end %>
<!-- end -->
<%= f.submit "Save" %>
<% end %>
You can simply use the array way if you want to send the data that doesn't belong to the model, in params hash inside page key. Here's how:
<%= text_field_tag 'page[header1]' %>
Not only this, if you would like to use another key, you can use that as well like <%= text_field_tag 'custom_key[header1]' %>
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 %>
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 %>
I'm having some troubles when I want to change the class of a form.
I have this partial:
<%= simple_form_for(#activity) do |f| %>
<%= f.input :name %>
<%= f.submit 'Guardar',:class=>"btn success" %>
<% end %>
In the browser the form label has class="simple_form activity"
According to Simple Form documentation, I can change the class by doing this
<%= simple_form_for(#activity, :defaults=>{:class=>"my_class"}) do |f| %>
But nothing is happening, what is wrong?
JavierQ
I think you want to use:
:html => { :class => "btn success"}
Instead of:
:class=>"btn success"