In Rails 3.2 - I sometimes use :input_html on forms.
For example:
<%= f.input :assign_client, :label => 'Charge Client?', :input_html => {:checked => true} %>
If the user unchecked the box and submits the form and there are some validation errors, the check box gets checked again.
Is there a way to leave it unchecked?
Thanks for your help!
UDPATE1
I changed the code to this:
<% if params.has_key?(:assign_client) %>
<%= f.input :assign_client, :label => 'Charge Client?' %>
<% else %>
<%= f.input :assign_client, :label => 'Charge Client?', :input_html => {:checked => true} %>
<% end %>
But, that didn't work.
The params hash will contain the assign_client key if the checkbox was checked by the user. So, you could do something like this:
<%= f.input :assign_client, :label => 'Charge Client?', :input_html => { :checked => params.has_key?(:assign_client) } %>
Related
How do I make a simple for boolean checkbox default to true?
assign_client is a boolean field.
I tried these:
<%= f.input :assign_client, :label => 'Charge Client?', :true %>
<%= f.input :assign_client, :label => 'Charge Client?', :value => :true %>
<%= f.input :assign_client, :label => 'Charge Client?', :value => 1 %>
Thanks for the help!
I think you should add input_html:
<%= f.input :assign_client, :label => 'Charge Client?', :input_html => { :checked => true }
proof
Your second one will work fine just remove the : so it's a boolean value rather than a symbol.
<%= f.input :assign_client, :label => 'Charge Client?', :value => true %>
I am using simple_form in rails and have a situation where I want the label to be the value from another field. In this case that field is not to be changed and so I don't want to be on the form.
To explain a bit better I have two lines that look like
<%= f.input :name, :label => false, :disabled => true, :input_html => { :class => 'input-small' } %>
<%= f.input :status, :collection => ["Not started", "Passed", "Failed"], :include_blank => false, :label => false %>
What I'd like to do is have the first element to be the label of the second element. Now I could do this by having them inline, but I'd like them to be lined up with the other elements so that the labels and inputs are lined up.
so doing something like
<%= f.input :status, :collection => ["Not started", "Passed", "Failed"], :include_blank => false, :label => f.name %>
or
<%= f.input :status, :collection => ["Not started", "Passed", "Failed"], :include_blank => false, :label => {f.input :name, :label => false, :disabled => true} %>
Any thoughts on how to get around this?
Michael
'f.object' gets the object associated to that form and then you can get to the fields:
<%= f.select(:status, [["Not started","Not started"], ["Passed", "Passed"], ["Failed", "Failed"]]), :label => f.object.name %>
In the end I did indeed go for the inline option, which isn't ideal but did the job. However I had to do the following.
The main form set as a norm form
The block below that set as form-horizontal
then the subform partial defined as a none simple_form
<div class="control-group form-inline">
<div class="controls">
<%= f.text_field :name, :disabled => 'true', :size => 10 %>
<%= f.select(:status, [["Not started","Not started"], ["Passed", "Passed"], ["Failed", "Failed"]]) %>
</div>
</div>
If it was set as a simpleform the inline would not work as required. Again not perfect, and certainly not elegant, but worked
I have a simple form:
<%= f.input :type, :required => true, :collection => ["Nonprofit","School","Company"], :hint => "Note: nonprofits will need to provide proof of nonprofit status", :input_html => { :value => params['type'] } %>
<%= f.input :name, :label => "Organization" %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email %>
A user gets to this page through a url like http://www.website.com/org/signup?type=Company
I can use this format to enter a value into a field like name or email, but can't figure out how to pass the param to the drop down.
I've already tried a few things including changing :value to :selected or :option but nothing seems to work.
Alright, figured it out! Posting here for future use.
<%= f.input :type, :required => true, :collection => ["Nonprofit","School","Company"], :hint => "Note: nonprofits will need to provide proof of nonprofit status", :selected => params['type'] %>
The trick is to drop the :input_html part and just use
:selected = > params['type']
Hope that helps someone in the future!
I made a form, but I don't know how to collect data from it and after pressing submit button redirect user to adding/fill.erb file, here is my form:
file: adding/counter.erb
<%= simple_form_for :counter do |f| %>
<%= f.input :first_name, :collection => 0..10 , :prompt => "How many?" %>
<%= f.input :last_name, :collection => 0..10 , :prompt => "How many?" %>
<%= f.input :city, :collection => 0..10 , :prompt => "How many?" %>
<%= f.input :postal, :collection => 0..10 , :prompt => "How many?" %>
<%= f.input :street, :collection => 0..10 , :prompt => "How many?" %>
<%= f.input :job, :collection => 0..10 , :prompt => "How many?" %>
<%= f.input :role, :collection => 0..10 , :prompt => "How many?" %>
<%= f.button :submit, 'next step', :style => "margin-top: 20px;" %>
<% end %>
I know it is a lame question, but I'm working on it for several hours and I don't know what to do:/
I'm not entirely clear, but it sounds like your trying to set the URL the form is posted to? If so, you should be able to do this:
<%= simple_form_for :counter, :url => {:controller => "counter", :action => "fill} do |f| %>
When the form gets submitted the flash message is displayed and the user remains at the same page with all fields filled.Now if one of the required fields id deleted and again we submit the form, the error is not displayed and the flash[:notice] is displayed.When i refresh the page the notice goes off.unable to figure out the reason .i have given the view code and in controller
flash[:notice] = "Saved Successfully"
View code:
<b><h3><%= flash[:notice] %></h3></b>
<% semantic_form_for(#featured_business, {:url => "#{#signin_link}".gsub(/\/+/, '/'), :html => {:multipart => true, :class => 'validate business'}}) do |f| %>
<% f.inputs do %>
<%= hidden_field_tag 'more_validations_required' %>
<%= f.input :name, :label => ' Name:' , :input_html => { :style => "width:240px;" }%>
<%= f.input :contact_name, :label => 'Contact name:',:required => false, :input_html => { :style => "width:240px;" } %>
<%= f.input :phone, :label => 'Phone Number:', :input_html => { :style => "width:240px;" } %>
<%= f.input :email, :label => 'Email:', :input_html => { :class => 'email' } , :input_html => { :style => "width:240px;" } %>
<%= f.commit_button :label => "", :button_html => {:class => 'signup business'} %>
<% end %>
<% end %>
Flash messages exist for the next page only. If you refresh the page then the flash message is no longer stored in the session.
Here is a good blog post to help you understand flash messages. http://travisonrails.com/2008/08/17/working-with-the-flash-hash