Simple Form collection, but exclude a specific record - ruby-on-rails

Here is the code I use to generate a select list of Companies.
<%= f.association :company, :collection => Company.order('name ASC'), :label => "Company: ", :include_blank => false %>
Now the table is pre-populated with a special record "id:1, name:none", I want to exclude this record as a selectable option in the select list that's generated. How can I go about accomplishing this?
Thanks!

Try this
<%= f.association :company, :collection => Company.where("id != 1").order('name ASC'), :label => "Company: ", :include_blank => false %>
Or
In the controller
#companies = Company.where("id != 1").order('name ASC')
In the view
<%= f.association :company, :collection => #companies, :label => "Company: ", :include_blank => false %>

Related

How can i save check boxes value to database in active admin?

I am new to ruby on rails and active admin, I need to create check boxes for registration fees. while I checked, the value from checkbox is not saved to the database. can anyone help me ??
form do |f|
f.inputs do
f.input :name, label: "Student Name"
f.input :dob,:label => "Date of Birth"
f.input :age,:label => "Age"
f.input :gender, as: :radio, :label => "Gender", :collection => [ "Male", "Female"]
f.input :reg_fee, :as => :check_boxes, :collection => RegChargeSetup.all.map{|v| ["#{v.reg_fee}"]}
end
f.actions
end
From your code, you are taking array of arrays, instead you should take array of strings,
So, change
f.input :reg_fee, :as => :check_boxes, :collection => RegChargeSetup.all.map{|v| ["#{v.reg_fee}"]}
to,
f.input :reg_fee, :as => :check_boxes, :collection => RegChargeSetup.all.map{|v| "#{v.reg_fee}"}
form do |f|
f.inputs do
f.input :name, label: "Student Name"
f.input :dob,:label => "Date of Birth"
f.input :age,:label => "Age"
f.input :gender, as: :radio, :label => "Gender", :collection => [ "Male", "Female"]
f.input :reg_fee, :as => :check_boxes, :collection => RegChargeSetup.all.map{|v| "#{v.reg_fee}"}
end
f.actions
end

Passing URL Parameter to Drop Down

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!

How to NOT save both parent and child record when associated child record is not valid in rails 3.1.4?

In our rails 3.1.4 app, there are user (parent) and user_levels (child) models.
class User < ActiveRecord::Base
attr_accessible :name, :login, :password, :user_levels_attributes, :as => :role_new
has_many :user_levels
ccepts_nested_attributes_for :user_levels, :allow_destroy => true, :reject_if => proc { |a| a['position'].blank? }
validates_associated :user_levels
end
class UserLevel < ActiveRecord::Base
belongs_to :user
validates_presence_of :position
end
The position column in user_level has to be filled. If it is not, both user and user_levels should NOT be saved. The problem with the above is that it always causes error of "position can't be blank" even there is a position value in the params.
{"utf8"=>"✓",
"user"=>{"name"=>"tester ceo",
"login"=>"testceo",
"update_password_checkbox"=>"[FILTERED]",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"user_levels_attributes"=>{"1339886115748"=>{"position"=>"ceo"}}},
"commit"=>"Save"}
After deleting user_levels_attribues, the model saves user into users table no matter what kind of user_levels is filled int. We did extensive online search and haven't found a solution yet. For example, if validates_presence_of :user_levels is added to the user model, then none of the user levels can be saved or updated, even with a valid position.
Any suggestion about how to implement the orchestrated saving/updating of two associated models? Thanks so much.
UPDATE:
_user_level.html.erb:
<div class="fields">
<%= f.input :position, :collection => return_position, :prompt => "Choose position",
:label => false, :include_blank => true, :selected => i_id %>
<%= link_to_remove_fields "remove", f %>
</div>
_form_new.html.erb:
<%= simple_form_for #user do |f| %>
<%= f.input :name, :label => 'name:' %>
<%= f.input :login, :label => 'login:', :hint => '6 digits ' %>
<%= f.input :password, :label => 'password:', :hint => '6digits', :input_html => {:id => 'new_user_password'} %>
<%= f.input :password_confirmation, :label => 'confirmation:' %>
<%= f.input :user_type, :label => 'user type:', :collection => return_user_type, :include_blank => true %>
position:
<p><%= link_to_add_fields "Choose position", f, :user_levels %></p>
<p><%= f.button :submit, 'Save' %></p>
<% end %>
Here is the method to add position field:
#add user position in system user creation
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render :partial => association.to_s, :locals => {:f => builder, :i_id => 0}
end
link_to_function(name, "add_fields(this, \"#{association}\", \"#{j fields}\")")
end
JS file:
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).parent().before(content.replace(regexp, new_id));
}
I think you need to remove the :reject_if and change validates_associated :user_levels to validates_presence_of :user_levels.

Rails ActiveAdmin, two columns (not sidebar) on form page

In a form in active admin, the fields expand horizontally across the page according to the size of the window. If it's a big monitor, there is a lot of "unused" space to the right.
How can I add a "column" (NOT a side bar) to the right of the page so that I will end up with a 50% width section of the form on the left and a 50% width section of the form on the right?
I need this because I have lots of fields.
This is what my form partially looks like right now...
form do |f|
f.inputs "Shipment Details" do
f.input :file_number
f.input :customer, :label_method => :company_name
f.input :shipper, :label_method => :company_name
f.input :broker, :label_method => :company_name
end
f.inputs "Places" do
f.input :place_of_origin, :as => :select, :collection => Place.find(:all, :order => "city", :select => "city").map(&:city)
f.input :place_of_loading, :as => :select, :collection => Place.find(:all, :order => "city", :select => "city").map(&:city)
f.input :place_of_delivery, :as => :select, :collection => Place.find(:all, :order => "city", :select => "city").map(&:city)
f.input :via, :as => :select, :collection => Place.find(:all, :order => "city", :select => "city").map(&:city)
end
f.inputs "Carrier" do
f.input :carrier, :label_method => :company_name
f.input :mode, :as => :select,
:collection => ["Air", "Air Collect", "Air Prepaid", "FCL", "FTL", "LCL", "LTL", "TBA"]
f.input :mbl, :label => "MBL"
f.input :hbl, :label => "HQL"
f.input :vessel
f.input :container
end
f.buttons
end
It's pretty simple, you can give each section of your inputs a CSS class, then you would modify active_admin.css.scss (or just .css) so it would float your forms to the correct place, as well as give them a correct width, etc.

rails formtastic habtm in and out

EDIT: looking for this: http://diminishing.org/extending-formtastic-with-a-sprinkle-of-jquery (If this works I'll answer my own question)
I've started to create an in and out type habtm (has many and belongs to many) form through formtastic. However I would like it to be more flexible to the user, meaning if you select a name on one side you either click a button that moves it right away (ajax on click) or after the form is submitted
(I'm using formtastic with this, but it doesn't have to in anyones examples)
What I am struggling with is the javascript, in and out buttons..
Comparison Model
class Comparison < ActiveRecord::Base
has_and_belongs_to_many :devices
end
Devices Model
class Device < ActiveRecord::Base
has_and_belongs_to_many :comparisons
end
Comparison Controller
def edit
#comparison = Comparison.find(params[:id])
#devices = Device.find(:all, :select => 'id, device_name', :order => 'device_name')
#devices_selected = #comparison.devices.find(:all, :order => 'device_name', :conditions => ["id IN (?)", #devices])
#devices_not_selected = Device.find(:all, :order => 'device_name', :conditions => ["id NOT IN (?)", #devices_selected])
end
Comparison Edit View
<% semantic_form_for #comparison do |f| %>
<% f.inputs do %>
<%= f.input :comparison_name %>
<%= f.input :description %>
<h3>
Select Devices
</h3>
<% f.inputs :class => 'inline_fields' do %>
<%= f.input :devices,
:collection => #devices_not_selected,
:label_method => :device_name,
:label => 'Add Devices',
:input_html => { :size => 20 },
:include_blank => false,
:hint => 'Select devices from this list to compare to the parent device' %>
<%= f.input :devices,
:collection => #devices_selected,
:label_method => :device_name,
:label => 'Remove Devices',
:input_html => { :size => 20 },
:include_blank => false,
:hint => 'Deselect devices from this list to take them off the comparison' %>
<% end %>
<% end %>
<%= f.buttons %>
<% end %>
I'll be using jquery to fix this problem:
http://quasipartikel.at/2009/05/10/jqueryui-multiselect/
or
http://blog.jeremymartin.name/2008/02/easy-multi-select-transfer-with-jquery.html#

Resources