Field named value in Rails - ruby-on-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 %>

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

DRY Rails Forms - Form Partials

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

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

Rails 3.1 - Displaying form values on Edit?

I feel like this should be really really simple, but I'm completely stuck!
In a form I might have a field like:
<%= f.text_field :name, :class => "text" %>
On edit, this pulls back in the value submitted on create, that's fine. But I want to prevent certain fields from being edited. I know I can disable the field or hide it, but I'd like to be able to pull in the values to display and use in other ways. How do I access them?
In this case I've tried things like:
<%= f.track.name %>
<%= track.name %>
<%= #track.name %>
But none of the above work!
Any ideas folks?
EDIT: (I'm using nested forms for this)
<%= form_for(#release, :html => { :multipart => true }) do |f| %>
<h3>Upload Tracks for <%= #release.title %></h3>
<%= f.fields_for :tracks do |builder| %>
<%= render 'upload_track_fields', :f => builder %>
<% end %>
<%= f.submit "Upload Tracks", :class => "submit" %>
<% end %>
And the upload_track_fields that are rendered:
<%= f.text_field :position, :class => "text" %>
<%= f.text_field :name, :class => "text" %>
<%= f.text_field :isrc, :class => "text" %>
<%= f.text_field :version, :class => "text" %>
<%= f.file_field :track, :class => "text" %>
<%= f.hidden_field :primary_genre_id %>
<%= f.hidden_field :secondary_genre_id %>
<%= f.hidden_field :alt_primary_genre %>
<%= f.hidden_field :alt_secondary_genre %>
<%= f.hidden_field :asset_tier %>
<%= f.hidden_field :preview_start %>
<%= f.hidden_field :parental_advisory %>
<%= f.hidden_field :available_separately %>
<%= f.hidden_field :_destroy %>
I've hidden most of the fields to prevent editing, but still need to see some of the fields so they're left as text fields. I tried to disable them, but that stops any changes (specifically the file upload) working.
In short, i'd prefer to display most of the above as text rather than form fields.
In the main form:
<% index = 0 %>
<% f.fields_for :tracks do |builder| %>
<%= #release.tracks[index].name %>
<%= render 'upload_track_fields', :f => builder %>
<% index += 1 %>
<% end %>
In the nested form:
<%= f.text_field :position, :class => "text" %>
# Notice that there's no "name" attribute
<%= f.text_field :isrc, :class => "text" %>
<%= f.text_field :version, :class => "text" %>
<%= f.file_field :track, :class => "text" %>
What I did in the first snippet is dirty, but I never used fields_for so I don't know how to get the actual index. I looked in Google, but I didn't find a solution so far.
I can't try it right now, I'll do it when I'll be home.
I suggest using this while finding a way to get the index.
Good luck!
As those who have commented said, I'd assume the <%= #track.name %> should work, if you have #track = #release.track (for instance) in your edit method in the controller.
Instead of keeping track of the index you can access the associated objects through builder, it's actually a track
<% f.fields_for :tracks do |builder| %>
<%= builder.object.name %>
<%= render 'upload_track_fields', :f => builder %>
<% end %>

Ruby on Rails - How to pass hash parameter to a partial view?

'shared/subscription' %>
To call this partial view:
<% form_for(:subscription, :url => city_subscriptions_path(#city)) do |form| %>
<%= form.hidden_field :city_id, :value => #city.id %>
<%= form.text_field :email, :size => 30 %>
<%= form.submit "Email Me" %>
<% end %>
Since I am using this partial view on different places, how do I alter the caller so it will pass a hash for the form_for helper? So it would be like this when the helper is called:
<% form_for(:subscription, :url => city_subscriptions_path(#city), :html => {:id => 'main_input' }) do |form| %>
<%= form.hidden_field :city_id, :value => #city.id %>
<%= form.text_field :email, :size => 30 %>
<%= form.submit "Email Me" %>
<% end %>
<%= render :partial => "shared/subscription", :locals => {:foo => "bar", :foofoo => ["bar", "bar"]}
In your partial view, use them:
<%= foo #this outputs "bar" %>
<%= foofoo.to_s %>

Resources