Flash[:notice] remains after form submission - ruby-on-rails

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

Related

Rails input_html being reapplied

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

Managing Roles with Checkboxes with Rolify

I currently have a partial that is able to manage roles via radio buttons below:
<%= simple_form_for user, :url => user_path(user), :html => {:method => :put, :class => 'form-horizontal' } do |f| %>
<h3>Change Role</h3>
<%= f.input :role_ids, :collection => Role.all, :as => :radio_buttons, :label_method => lambda {|t| t.name.titleize}, :label => false, :item_wrapper_class => 'inline', checked: user.role_ids %>
<%= f.submit "Add or Remove Role", :class => "btn" %>
<% end %>
I want to change this so i can add or remove multiple roles using checkboxes.
<%= simple_form_for user, :url => user_path(user), :html => {:method => :put, :class => 'form-horizontal' } do |f| %>
<h3>Change Role</h3>
<%= f.input :role_ids, :collection => Role.all, :as => :check_boxes, :label_method => lambda {|t| t.name.titleize}, :label => false, :item_wrapper_class => 'inline', checked: user.role_ids %>
<%= f.submit "Add or Remove Role", :class => "btn" %>
<% end %>
But this seems to not work, Here is the console response when using radio buttons:
{"utf8"=>"✓", "authenticity_token"=>"MTV5MNomlkV86ynh0SVR6OW5SG3+9BCznWqcfq2xaWM=", "user"=>{"role_ids"=>"2"}, "commit"=>"Add or Remove Role", "id"=>"4"}
and when using checkboxes:
{"utf8"=>"✓", "authenticity_token"=>"MTV5MNomlkV86ynh0SVR6OW5SG3+9BCznWqcfq2xaWM=", "user"=>{"role_ids"=>["2", "3", "7", ""]}, "commit"=>"Add or Remove Role", "id"=>"2"}
So why isn't it saving to the db? why do i have an empty spot in my array at the end?

contact_us gem not mailing

I'm just trying to send email from within my application. For some reason, the email isn't being sent. Am I missing a step? Here is the contact_us documentation. I followed it exactly. https://github.com/jdutil/contact_us. How can I get it to mail?
Here is my view:
<div class="container">
<h2><%= t('.contact_us') %></h2>
<%= simple_form_for #contact, :url => contacts_path do |f| %>
<%= f.input :name, :label => t('.name') if ContactUs.require_name %>
<%= f.input :email, :label => t('.email') %>
<%= f.input :subject, :label => t('.subject') if ContactUs.require_subject %>
<%= f.input :message, :as => :text, :label => t('.message') %>
<%= f.button :submit, :value => t('.submit'), :alt => t('.submit'), :id => 'contact_us_contact_submit', :title => t('.submit') %>
<% end %>
</div>
I also set config.mailer_to = "drichards2013#gmail.com."
When I submit the form, I receive a notification that the email was successfully sent.

Updating paperclip avatar in multipart simple_form

I would like to create an edit page for the below form. The problem is that when the user browses to the edit page the brand_name and name are pre-filled, but the image upload field shows 'no file chosen' even when an avatar exists for the 'style'. Please let me know if there is some way to remedy this. Thanks!
My Edit Form:
<%= simple_form_for #style, :html => { :class => 'form-horizontal' }, :remote => true do |m| %>
<%= m.input :brand_name, :label => 'Brand', :placeholder => 'Brand' %>
<%= m.input :name, :label => 'Style', :placeholder => 'Style' %>
<%= m.input :avatar, :label => "Image" %>
<div class="form-actions" style = "background:none">
<%= m.submit nil, :class => 'btn btn-primary' %>
<%= link_to 'Cancel', styles_path, :class => 'btn' %>
</div>
<% end %>
Just implemented this yesterday. Make a custom input in /app/inputs
class AvatarInput < SimpleForm::Inputs::FileInput
def input
out = '' # the output string we're going to build
# check if there's an uploaded file (eg: edit mode or form not saved)
if object.send("#{attribute_name}?")
# append preview image to output
# <%= image_tag #user.avatar.url(:thumb), :class => 'thumbnail', id: 'avatar' %>
out << template.image_tag(object.send(attribute_name).url(:thumb), :class => 'thumbnail', id: 'avatar')
end
# append file input. it will work accordingly with your simple_form wrappers
(out << #builder.file_field(attribute_name, input_html_options)).html_safe
end
end
Then you can do
<%= f.input :avatar, :as => :avatar %>
This is all I needed for this to work (in haml):
=simple_form_for #user, :html => {:multipart => true } do |f|
=f.file_field :image
The code for new/edit views from paperclip's github page looks like this:
<%= form_for :user, #user, :url => user_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %>
<% end %>
So maybe you should try m.file_field and include :html => { :multipart => true } as well? Though I personally prefer Attachment-Fu.

how to bind custom action to submit button on a form?

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

Resources