How to render a select tag with an option marked as selected - ruby-on-rails-3

<div class="field">
<%= f.label :category %><br />
<%= select_tag "category", options_from_collection_for_select(#categories, "id", "name"), :prompt => "Select something" %>
<% if not #category_id.nil? %>
<script>
$("#category option").each(function(){
if ( this.value == <%= #category_id %> )
this.selected = true;
});
</script>
<% end %>
</div>
I wanna make the category <select> tag have the right value (#category_id).
I try adding some script to category.js.coffee, but I found I can't pass the #category_id to the coffee file. So the script is inline.
I don't know any other ways to solve the problem. I just think my code is ugly.
Can anybody have any other solutions about my question.
If you were me, which method will you use. Thx.
Regards,
Rails newbie

You should be able to render one of the options as pre-selected without javascript. Try options_from_collection_for_select(#categories, "id", "name", #category_id).

Related

Pass a variable to text_field without changing form

I have _form.html.erb
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :password %>
<%= f.password_field :password %>
Now if I render this form in homepage, HTML code should be:
<label for="session_name">Name</label>
<input id="session_name" name="session[name]" size="30" type="text">
...
If I change my _form.html.erb to:
<%= f.label :name %>
<%= f.text_field :name, disabled: true %>
...
HTML code should be:
...
<input disabled="disabled" id="session_name" name="session[name]" size="30" type="text">
...
But, I don't want to change my _form.html.erb, so how can I pass the disabled: true into my form? I tried to use render partial: but don't know syntax.
I just learn Ruby on Rails for 2 weeks, so please help me.
i donno why u wnt to do so but u can solve the prob by this method. Try doing-
in application helper, add method -
def add_disabled
#i suppose that u want the field to be disabled in the users controller for edit action but not for other controllers or actions
return "disabled='disabled'" if params[:controller] == "users" and params[:action] == "edit"
end
in _form.html.haml
= f.text_field :name, #{add_disabled}
this will call the helper method and return "disabled='disabled'" depending upon the controller and action

Specifying jquery mobile data themes within a Rails 3.0 form

I would like to specify a jquery mobile theme for an element in a rails 3.0 form. The following doesn't work.
<div data-role="page" data-theme="a">
... #content formatted with data theme a
<div data-role="checkbox" data-theme="c">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
... #content formatted with data them a
</div>
Thanks!
UPDATE: shanabus solution in rails format -
<%= f.label :remember_me, { 'data-theme' => 'c' } %>
In order for you to specify the theme of a checkbox in jQuery Mobile, you would have to set it directly on the checkbox element. Could be something like this:
<%= f.check_box("label", "accepted", { 'data-theme' => 'c' }, "yes", "no") %>
Here is the docs for jQuery Mobile: http://jquerymobile.com/test/docs/forms/checkboxes/options.html
And for a Rails example of adding attributes to a check_box: http://apidock.com/rails/ActionView/Helpers/FormHelper/check_box
Hope this helps!

ActsAsTaggableOn Tagging with checkboxes

I'm playing with ActsAsTaggableOn in a small project to see what are the possibilities of this gem.
So I have Products and products can have tags.
In my Product _form
<%= form_for(#product) do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
Tags<br />
<% tag_cloud(#tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
<%= check_box_tag 'product[tag_list][]',tag.name,#product.tag_list.include? (tag),:class => css_class %>
<%= tag.name %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
In ProductController I have defined the tag_cloud method
def tag_cloud
#tags = Product.tag_counts_on(:tags)
end
So in my editing product, I can tag the product with the checkboxes, but if a product already has tags those checkboxes aren't selected. I assume that here #product.tag_list.include? (tag) I am missing something or doing something wrong.
Any ideas? Thanks for your help =)
Cheers.
Today, after reading more carefully, I found the answer to this one.
This line
<%= check_box_tag 'product[tag_list][]',tag.name,#product.tag_list.include? (tag),:class => css_class %>
should be
<%= check_box_tag 'product[tag_list][]',tag.name,#product.tag_list.include? (tag.name),:class => css_class %>
So instead of the object I want to check if that name exists in the list.
Hope this helps someone.

RoR Difficulty accessing params in controller

I have an edit page with the following code, basically a dropdown and button that calls update.
<% form_tag('switch_car', :method => :put) do%>
<div class="field">
<label>Car Name:</label>
<%= select("params", ":id", #available_cars.collect {|v| [v.name, v.id]})%>
</div>
<div>
<%= submit_tag "Switch Car" %>
</div>
<% end %>
The server reads like the params is being set:
Parameters: {"utf8"=>"Γ£ô", "authenticity_token"=>"8vHXrnICaOKrGns6FfMUcd/dWo5kpNKpA8F5l5ozRkY=", "params"=>{":id"=>"9"}, "commit"=>"Switch Car"}
However, when I put the params into a session I get nothing. It seems to be always nil. Not sure what I am doing wrong? Here is code in the controller.
def update
if params[:id]
session[:car_info_id] = params[:id]
redirect_to entry_url
else
redirect_to switch_car_path
end
end
It always gets redirected to the switch_car_path so I am assuming params[:id] is always nil. When I put if params[:id] == nil it goes to the entry_url.
Thanks in advance.
you want params[:params][":id"]
Alternatively, you could put this in your view:<%= select("car_info", "id", #available_cars.collect {|v| [v.name, v.id]})%>
And then in your controller:if params[:car_info][:id]
While the other answer would work, this is probably what you'd want to be doing (using select_tag(:id) will automatically add an :id key/value to the params hash):
<% form_tag('switch_car', :method => :put) do %>
<div class="field">
<label>Car Name:</label>
<%= select_tag(:id, options_from_collection_for_select(#available_cars, "id", "name")) %>
</div>
<div>
<%= submit_tag "Switch Car" %>
</div>
<% end %>
Then you can easily access params[:id] in the controller.

Translating label for references collection_select

i have form with one selectbox (collection_select) for references column. What is proper way to translate label for this widget? For all others a use default Model.human_attribute_name (my code, Translations for Active Record Models)
If you use in your [language].yml file:
attributes:
my_model:
property: 'translatet property label'
[...]
You can simply use the following in your view:
<%= form_for([...]) do |f| %>
<div class="field">
<%= f.label :property %><br />
<%= f.collection_select [...] %>
</div>
[...]