How do I translate the following to simple form?
<%= form_for(#bill) do |f| %>
<%= f.label :location_id %>
<%= f.collection_select(:location_id, #locations, :id, :location_name,
{:selected => #bill.location_id}) %>
I can't use a simple association because #locations is the result of a where query.
Try this
<%= simple_form_for #bill do |f| %>
<%= f.input :location,:collection => #locations,:label_method => :location_name,:value_method => :id,:label => "Location" ,:include_blank => false %>
<%= f.button :submit %>
<%end%>
Hope this help
Here is the final:
<%= f.input :location_id, collection: #locations, label_method: :location_name, value_method: :id,label: "Location", include_blank: false, selected: #bill.location_id %>
In simple form, if we have an association between location and bill, than we can do like this:
<%= simple_form_for #bill do |f| %>
<%= f.association :location, collection: #locations, priority: #bill.location %>
<%= f.button :submit %>
<% end %>
Hope this will help.
In your location model, you can also create a :
def to_label
location_name
end
Related
I'm using simple_form to create my scaffolding forms in my current project. I need some help to improve my address CRUD. After my db:seed there are lots of cities registered.
My current form for address is:
<%= simple_form_for #address, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :street%>
<%= error_span(#address[:street]) %>
<%= f.input :zip%>
<%= error_span(#address[:zip]) %>
<%= f.label :city_id %>
<%= f.collection_select(:city_id , City.all, :id, :name, prompt: true) %>
<%= error_span(#address[:city_id]) %>
<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
address_path, :class => 'btn btn-default' %>
<% end %>
How can I filter my citties collection bellow selecting first the state ?
<%= f.collection_select(:city_id, City.all, :id, :name, prompt: true) %>
Thanks
Add this class method to your model:
def self.by_state
order('state DESC')
end
Then use it in your form:
<%= f.collection_select(:city_id , City.by_state, :id, :name, prompt: true) %>
I'm using 'rails-settings-cached' gem and I want to add ability of creating/updating settings in backend. Model has fields: 'var', 'value', etc... As form builder I'm using Formtastic-bootstrap.
When I doing f.input :value I get no implicit conversion of nil into String. I think that the reason is that 'value' is a keyword.
How can I solve my problem?
Thank you!
UPD:
<%= semantic_form_for [:admin, #setting], :html => {:class => "form-horizontal"} do |f| %>
<%= f.semantic_errors :var, :value%>
<%= f.inputs do %>
<%= f.input :var %>
<%= f.input :value, :as => :text %>
<% end %>
<%= f.actions :class => 'form-group' do %>
<div class="col-lg-offset-2 col-lg-8">
<%= f.action :submit %>
<%= f.action :cancel %>
</div>
<% end %>
I'm having problems with repetetive Rails form codes. When I put them in a partial they are throwing NoMethodError and I couldn't figure it out how to make them DRYer.
admin/exam_centers/edit.html.erb =>
<%= simple_form_for(#exam_center, url: admin_exam_center_path(#exam_center)) do |f| %>
<%= f.input :city_id, collection:City.all, label: 'Şehir Seçiniz', label_method: :name %>
<%= f.input :address, label: 'Adres' %>
<%= f.input :building, label: 'Bina' %>
<%= f.button :submit, "Kaydet", :class => "btn btn-info" %>
<% end %>
Notice: admin_exam_center_path(#exam_center)
admin/exam_centers/new.html.erb =>
<%= simple_form_for(#exam_center, url: admin_exam_centers_path(#exam_center)) do |f| %>
<%= f.input :city_id, collection:City.all, label: 'Şehir Seçiniz', label_method: :name %>
<%= f.input :address, label: 'Adres' %>
<%= f.input :building, label: 'Bina' %>
<%= f.button :submit, "Kaydet", :class => "btn btn-info" %>
<% end %>
Notice: admin_exam_centers_path(#exam_center)
They are working perfect like this. But when I put them in a partial as you can see below, the new.html.erb page is throwing "undefined method exam_centers_path" error and the edit.html.erb page is throwing "undefined method exam_center_path" error. How can I make these form DRYer?
_form.html.erb =>
<%= simple_form_for(#exam_center) do |f| %>
<%= f.input :city_id, collection:City.all, label: 'Şehir Seçiniz', label_method: :name %>
<%= f.input :address, label: 'Adres' %>
<%= f.input :building, label: 'Bina' %>
<%= f.button :submit, "Kaydet", :class => "btn btn-info" %>
<% end %>
edit.html.erb =>
<%= render 'admin/exam_centers/form' %>
new.html.erb =>
<%= render 'admin/exam_centers/form' %>
By the way, the routes file =>
namespace :admin do
resources :exam_centers, except: :show
resources :exam_languages, except: :show
end
It looks like you just need to use a polymorphic url builder to specify the :admin piece of your route. Try specifying the form helper like this:
<%= simple_form_for([:admin, #exam_center]) do |f| %>
This will allow Rails to tack on the admin_ piece of the named route before generating the rest.
You only put common part in partials so you can do something like this:
new.html.erb
<%= simple_form_for(#exam_center, url: admin_exam_centers_path(#exam_center)) do |f| %>
<%= render partial: "form", locals: {:f => f} %>
<% end %>
edit.html.erb
<%= simple_form_for(#exam_center, url: admin_exam_center_path(#exam_center)) do |f| %>
<%= render partial: "form", locals: {f: "f"} %>
<% end %>
_form.html.erb
<%= f.input :city_id, collection:City.all, label: 'Şehir Seçiniz', label_method: :name %>
<%= f.input :address, label: 'Adres' %>
<%= f.input :building, label: 'Bina' %>
<%= f.button :submit, "Kaydet", :class => "btn btn-info" %>
in my rails view I have a form with a dropdown that looks like this:
<%= simple_form_for(#appointment) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.association :client, label_method: lambda { |c| "#{c.first_name} #{c.last_name}" } %>
<%= f.input :occured_on %>
<%= f.input :start %>
<%= f.input :end %>
<%= f.input :copay_received %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
I would like to limit the contents of the dropdown to only the clients associated with current_user.id.
How do I accomplish this?
Let me know if you need to see my controller or something else but it does nothing fancy. I bet you can guess it's contents.
Just add the collection argument:
<%= f.association :client, label_method: lambda { |c| "#{c.first_name} #{c.last_name}" }, collection: Client.where(user_id: current_user.id) %>
Alternative:
# this implies you have declared: user has_many :clients
<%= f.association :client, collection: current_user.clients, label_method: lambda{|c| "..."} %>
I am trying to create an element in my form that uses simple form+bootstrap. The idea is to allow a user select a type of currency from a drop down.
Customer_currency to select from either USD- US Dollars, LRD - Liberian dollar among others.
I have used the following in my form
However, it is not working, all I see is a drop down (out of position) in my form with the options but no label.
How can i create a good select element with a label using simple form
<%= simple_form_for #customer, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :name %>
<%= f.select :customer_currency, [['UGX- Uganda Shillings',1],['USD- US Dollars',2]] %>
<%= f.input :payment_terms %>
<%= f.input :billing_address %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :mobile %>
<%= f.input :email %>
<div class="form-actions">
<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
customers_path, :class => 'btn' %>
</div>
<% end %>
<%= f.input :customer_currency, :collection => [['UGX- Uganda Shillings',1],['USD- US Dollars',2]] %>
Add label_method and value_method to your select, means change:
<%= f.select :customer_currency, [['UGX- Uganda Shillings',1],['USD- US Dollars',2]] %>
to:
<%= f.select :customer_currency, [['UGX- Uganda Shillings',1],['USD- US Dollars',2]], label_method: :first, value_method: :last %>
Update: other solution
<%= f.input :customer_currency, as: :select, [['UGX- Uganda Shillings',1],['USD- US Dollars',2]], label_method: :first, value_method: :last %>