DRY Rails Forms - Form Partials - ruby-on-rails

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

Related

Filter cities by state in Simple_form Ruby on Rails

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

Field named value in Rails

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

How to submit a form using simple_form_for when what's being submitted isn't part of the model

I'm trying to submit a form that contains fields for both an event and an invitation to that event. Here is my form:
<%= simple_form_for(#event) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title %>
<%= f.input :description %>
<%= f.input :start_at %>
<%= f.input :end_at %>
<%= f.input :all_day %>
<%= f.hidden_field :owner_id, value: current_user.id %>
<% if false %>
<%= f.association :sitter, label_method: lambda { |s| "#{s.name}" }, collection: User.all %>
<%= f.association :group, label_method: lambda { |g| "#{g.group_name}" }, collection: Group.where(:owner_id => current_user.id) %>
<% end %>
<%= f.input :user_emails, as: :text %>
<%= form_tag event_invitations_path, :method => :post do %>_tag :user_emails %>
</div><div>
<%= label_tag "Your message:" %>
</div><div>
<%= text_area_tag :email_message %>
<% end %>
</div>
</br>
<div class="form-actions">
<%= f.button :submit, :class => 'btn-primary' %>
</div>
<% end %>
This is (perhaps obviously) not working. :user_emails is NOT part of the event or invitation model as it's a list of emails that will be used to create invitations. Basically I merged two forms, one that was accepting the email/send invitation piece and one that was accepting the event information. I think I have my controller and model set up properly to take care of this but how do i submit this information as part of the same form without getting an "undefined_method" error (since user_emails doesn't belong to events). Let me know if you want to see my model/controller.
Use FormTagHelper
<%= label_tag "User", "Email"%>
<%= text_area_tag "user_emails", "example#example.com"%>
See the documentation for more options on text_area_tag and label_tag

Rails 4 simple_form collection_select

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

Select element, simple_form helper

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

Resources