Simple Form: Non object related attribute. How to? - ruby-on-rails-3

I have a simple form that looks like this:
<%= simple_form_for #study,:url => studies_path, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :category, :collection => #categories, :label => "Category", :selected => #categories.first %>
<%= f.button :submit, t('add_form'),:class => 'btn-primary' %>
I want to add a new field in that form that will be merely visual sugar. It will be a helper to dynamically change the :category field.
How do I do that?

Use text_field_tag or whichever other _tag helper to generate a form element without attaching it to any specific attribute of the model:
<%= simple_form_for #study,:url => studies_path, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :category, :collection => #categories, :label => "Category", :selected => #categories.first %>
<%= text_field_tag :blah %>
<%= f.button :submit, t('add_form'),:class => 'btn-primary' %>
<% end %>

Related

How to disable certain check_boxes when using Formtastic's :as => :check_boxes

<%= semantic_form_for [:admin, #admin] do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :email, :as => :email %>
<%= f.input :password, :as => :password %>
<%= f.input :password_confirmation, :as => :password %>
<%= f.input :admin_roles, :as => :check_boxes, :required => true, :disabled => [1] %>
<% end %>
<%= f.actions %>
<% end %>
-
<%= f.input :admin_roles, :as => :check_boxes, :required => true, :disabled => [1] %>
Passing an array to the above code disables the checkbox with id=1, which is nice. But I want to check the model of each checkbox, too see if it has a specific value, so I can disable it or not.
do_not_disable_checkbox if admin_role.do_not_show_me_boolean_field
Do I need to iterate each of my :admin_roles and output a checkbox for each? Or can I do it in one line like above? I'm new to rails and ruby, and I can't wrap my head around it and hoping for some help to put me in the right direction.

Rails 3 Mass-Assignment Errors with fields_for

I have the following model relationships:
OrderModel:
has_one :credit_card
accepts_nested_attributes_for :credit_card
attr_accessible :user_id, :date_updated, :date_finished, :amount, :payment_method, :status, :billing_cycle, :auth_net_subscription_id, :billing_start_date, :credit_card_attributes, :billing_address_id, :cc_id
CreditCardModel:
belongs_to :order
Here is my Order Controller (orders#checkout)
def checkout
#order = current_order
#cc = CreditCard.new
#order.build_credit_card
respond_with #order
end
Here is the form for entering in a CC on the order:
<%= form_for(#order, :url => finish_checkout_path, :html => { :class => 'validate' }) do |f| %>
<%= f.fields_for #cc do |cc| %>
<%= cc.text_field :cc_number, :placeholder => "Credit Card Number", :class => "full-width validate[required, creditCard] cc" %>
<%= cc.text_field :name, :placeholder => "Name as it appears on card", :class => "full-width validate[required]" %>
<%= select_month(Date.today, {:field_name => 'exp_month', :prefix => "order[credit_card]", :prompt => "EXP. MONTH"}, { :class => "dk half-width validate[required,past] marginRight10" }) %>
<%= select_year(Date.today, {:field_name => 'exp_year', :prefix => "order[credit_card]", :prompt => "EXP. YEAR", :start_year => Date.today.year, :end_year => Date.today.year + 10}, { :class => "dk half-width validate[required]" }) %>
<%= link_to "What's this?", "#", :class => 'cvv-help' %>
<%= cc.text_field :cvv, :class => 'half-width validate[required] marginRight10', :placeholder => "CVV" %>
<%= cc.text_field :zip_code, :class => 'half-width validate[required]', :placeholder => "Zip Code" %>
<% end %>
<%= f.hidden_field :amount, :value => #order.products.collect(&:price).reduce(&:+) %>
<p class="tos">By clicking the button below you agree to our terms of service.</p>
<p class="align-center"><%= f.submit "Submit", :class => 'btn submit' %></p>
<% end %>
And here is where I update the order (orders#finish):
current_order.update_attributes(params[:order])
When I do this, I get the following error: Can't mass-assign protected attributes: credit_card
I clearly have the credit_card_attributes in my attr_accessibleso I am not sure why this is erroring out.
Not sure, but it might be because of your controller code:
def checkout
#order = current_order
#cc = CreditCard.new
#order.build_credit_card
respond_with #order
end
With this, the credit card that you use in your fields_for is not linked to your order. That might be the problem.
Try doing that:
def checkout
#order = current_order
#order.build_credit_card
respond_with #order
end
and
<%= f.fields_for :credit_card do |cc| %>

Rails is not creating hidden field for put method on an update form

I have a REST resource called Greetings.
Here is my routes file:
resources :greetings
I have a form to create or update a greeting as follows:
<%= form_tag(#greeting, :remote => true, :id => 'greeting_form') do %>
<%= text_area :greeting, :content, :rows => 3, :placeholder => "Type your message..." %>
<% end %>
note: I am using form_tag because this form also collects user data.
This is supposed to create a hidden field with method=> put but its not so it can't find the route.
Any ideas how I can get this to submit to the update action?
Just write
<%= form_tag(#greeting, :remote => true, :id => 'greeting_form', :method => :put) do %>
and everything should be working.
You can use form_for tag and still collect user data like this:
<%= form_for #greeting, :validate => true do |f| %>
<%= f.text_area :content, :rows => 3, :placeholder => "Type your message..." %>
<%= f.fields_for #user do |u| %>
<%= u.text_field :name %>
<% end %>
<% end

haml to html.erb

Can anyone please convert these haml code snippets to the equivalent html.erb?
1.
%h1
Edit Project Form
.edit_project
= semantic_form_for [:admin, #project], :url => admin_organization_project_path(#organization), :html => { :multipart => true } do |f|
= f.inputs do
= f.input :name
= f.input :status, :as => :select, :collection => Project.statuses
= f.input :overview
= f.input :funds_purpose
= f.input :goal
.files
= render :partial => 'admin/edit_photo', :collection => #project.project_photos, :locals => { :field_name => 'project[project_photos_attributes][][file]' }
= f.submit 'Save Project'
2.
%li.file.optional#project_project_photo_file_input
= label_tag 'File'
= image_tag edit_photo.file.url(:thumb) if edit_photo.file?
= file_field_tag field_name
For the second:
<li class="file optional" id="project_project_photo_file_input">
<%= label_tag 'File' %>
<%= image_tag edit_photo.file.url(:thumb) if edit_photo.file? %>
<%= file_field_tag field_name %>
</li>
for first:
<h1> Edit Project Form </h1>
<div class='edit_project'>
<%= semantic_form_for [:admin, #project], :url => admin_organization_project_path(#organization), :html => { :multipart => true } do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :status, :as => :select, :collection => Project.statuses%>
<%= f.input :overview %>
<%= f.input :funds_purpose %>
<%= f.input :goal %>
<div class='files' >
<%= render :partial => 'admin/edit_photo', :collection => #project.project_photos, :locals => { :field_name => 'project[project_photos_attributes][][file]' } %>
</div>
<% end %>
<%= f.submit 'Save Project' %>
<% end %>
</div>

send data to an array from 2 collection select boxes

I have controller with an action step2 which collects all devices by selected category. My step2.html.erb looks like:
<% form_for compare_comparision_path, :url => {:action => 'comparision'} do |f| %>
<%= f.collection_select(:device, #devices, :id, :name, options ={:prompt => "Select"}, :class=>'device') %>
</br>
<%= f.collection_select(:device, #devices, :id, :name, options ={:prompt => "Select"}, :class=>'device') %>
<%= f.submit 'ok' %>
<% end %>
I want for it to allow the user to select two devices and send it to some array or variable in comparison action.
You can do so:
<% form_for compare_comparision_path, :url => {:action => 'comparision'} do |f| %>
<%= f.collection_select('device[]', #devices, :id, :name, options ={:prompt => "Select"}, :class=>'device') %>
</br>
<%= f.collection_select('device[]', #devices, :id, :name, options ={:prompt => "Select"}, :class=>'device') %>
<%= f.submit 'ok' %>
<% end %>
and in controller you`ll have an array in params[:device] containing to selected values.
Or you can replace 'device[]' in my example with unique name for each selectbox (for example 'devise1' and 'device2'.
Then you can get selected values in controller by accessing params[:device1] and params[:device2]