I'm Rails newbie.
How to make a form which allows user to choose a language(en,fr etc) through Radio buttons in Home#Index View to Submit to Home#Language action ?
Thanks in advance
<%= form_tag language_path, :method => :post do %>
<%= label_tag :language_english, 'English' %>
<%= radio_button_tag :language, 'english' %>
<%= label_tag :language_french, 'French' %>
<%= radio_button_tag :language, 'french' %>
<%= submit_tag %>
<% end %>
Where language_path is the path defined in your routes.rb, such as
match "/home/language" => "home#language", :as => 'language'
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 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 %>
<% semantic_form_for(#product, :html => {:multipart => true}) do |f| %>
<% f.inputs do %>
<%= f.input :name %>
<%= f.input :price %>
<%= f.input :pno %>
<%= f.input :description %>
<%= f.input :shop_category %>
<% end %>
<% end %>
Product belongs to Shop_category, Shop_category belongs to Shop.
How to change the line :
<%= f.input :shop_category %>
To show only shop_categories that belongs to Shop with id for example 15 instead of showing all shop_categories in the select box ?
There's a :collection option for the select input.
<%= form.input :shop_category, :collection => #shop.ShopCategories %>
So you can, by providing a Hash to that collection attribute, display the categories you need, with the required conditions.
Also, if you set the shop_category in the controller, it will already be selected as a default value.
I am attempting to use form_for to implement a search form that works with a table-less Search model I created. The search form keeps triggering the 'index' action. I assume I should use 'new' to create the form and 'create' the process the search query. Looking at the log, my POST is getting changed into a GET. Here's my code:
/searches/new.html.erb:
<% form_for :searches, #search, :url => searches_path, :html => {:method => :post} do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :keywords %><br />
<%= f.text_field :keywords %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>
What's the standard way for triggering the 'create' action with form_for?
Are you using the RESTful map.resources :searches ?
If so, shouldn't your :url be set to new_search_path ?
form_for is used with models. For a simple search form, I reccommend doing something like this:
<% form_tag posts_path, :method => :get do |f| %>
<%= f.text_field :query %>
<% end %>
You'll get /posts?query=wtf.