Rails - Simple Form - Not loading class option - ruby-on-rails

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"

Related

No method error occurs in rails while creating a new text_field

I am trying to add a text field via form and setting the name as code.
My form calls a action verify in users controller. But when I run it
shows no method error code. I am new to rails can anyone help. I need to pass code as a params. I already created a user sign up and signin page.
<%= form_for(#user, :action => 'verify') do |f| %>
<%= f.label :code %>
<%= f.text_field :code %>
<%= f.submit "Verify", class: "btn btn-large btn-primary" %>
// a action in usercontroller
def verify
end
The user model probably does not have a code field. So the application is giving an error. If you just want to send code, use form_tag instead of form_for.
<%= form_tag verify_action_path do %>
<%= label_tag :code %>
<%= text_field_tag :code %>
<%= submit_tag "Verify", class: "btn btn-large btn-primary" %>
<% end %>

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 - two forms on one page

I have two forms on one view page. One of these is an update form. The other form is a single button that goes to the same controller action but does not actually update the form. Here is the first form:
<%= form_for [#project, #schedule] do |f| %>
<%= f.fields_for :tasks do |builder| %>
<%= render 'task_fields', :f => builder %>
<% end %>
<p><%= link_to_add_fields "Add task", f, :tasks %>
<p><%= f.submit "Submit" %></p>
<% end %>
And here is the second form:
<%= form_tag project_schedule_path(#project, #schedule), method: "patch" do %>
<div><%= hidden_field_tag :emp_accepts, true %></div>
<%= submit_tag "Accept schedule", class: "btn btn-large btn-primary" %>
<% end %>
The problem is the strong parameters. The strong parameters require :schedule, which only the first form provides. So when I try to use the second form, an error is returned. Here are the strong params:
def schedule_params
params.require(:schedule).permit(:emp_accepts,
tasks_attributes: [:title, :content, :_destroy])
end
you should change the second form to
<%= form_for [#project, #schedule], http: { method: 'patch' } do |f| %>
<div><%= f.hidden_field :emp_accepts, value: true %></div>
<%= f.submit 'Accept schedule', class: 'btn btn-large btn-primary' %>
<% end %>
It looks like you should really use different controller methods for each action. It would make it easy for you to make changes in the future.

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

make boolean form field hidden

I have a boolean to make comments public or private.
The boolean is a column in submissions and I have it working in a clumsy way right now and would like to eliminate the checkbox from the form, replacing with a hidden field, so that all the user sees is the submit button, conditional based on the boolean state:
submissions#show:
<% if #submission.comment_show == true %>
<%= render "hide_comment_form" %>
<%= render "comments/comment" %>
<% else %>
<%= render "show_comment_form" %>
</div>
<% end %>
_show_comment_form
<%= simple_form_for [#contest, #submission] do |f| %>
<div>
<%= f.input :comment_show, label: false %>
<%= hidden_field_tag :contest_id, #contest.id %>
<%= f.submit "Make Comments Public", :class => 'btn btn-mini' %>
</div>
<% end %>
_hide_comment_form
<%= simple_form_for [#contest, #submission] do |f| %>
<div class ="">
<%= f.input :comment_show, label: false %>
<%= hidden_field_tag :contest_id, #contest.id %>
<%= f.submit "Make Comments Private", :class => 'btn btn-mini' %>
</div>
<% end %>
I have tried hidden_field_tag, but haven't had any luck getting it to work.
Also, I've seen some fancier methods and routing to accomplish the same thing:
http://buckybits.blogspot.com/2011/09/simple-ajax-property-toggle-in-rails-30.html
But I would prefer to use a hidden field and conditionals to keep it simple.
Is it possible to use a hidden field to set a boolean in a form or do I have to go with a custom method and routing?
See the answer to this SO question: rails simple_form - hidden field - create?
Since you are using simple form, you can do something like this:
f.input :contest_id, :as => :hidden, :input_html => { :value => #contest.id }

Resources