I want to populate values fetched from a database to a drop down list, using rails 3:
#details contains all the values from the database. While printing #details.inspect it displays in the web page as:
[<DeviceDetail DeviceID: 14448, No: 616">, <DeviceDetail DeviceID: 14448, No: 617">, <DeviceDetail DeviceID: 14448, No: 618">]........
In the loop I fetch the No details. How can I show No in a drop down list?
<% #details.each do |d| %>
<%=d.No%>
<% end %>
I added the following for displaying No in drop down list, but it's returning the error undefined method name for DeviceDetail:0x390f3f0. My database does not contain name filed, so what should I put in the :name field? The database table contains no, deviceid, speed and time.
<% #details.each do |d| %>
<%=collection_select(:device_detail, d.No, #details, :id, :name) %>
<% end %>
It this possible?
Is this part of a form? Use the select_tag, and select the input option for the same.
Check out http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag.
If I understand your question correctly, you will need to declare the instance variable #details in your Device controller. It should look something like this:
controller/device.rb:
def show
#details = Detail.all
end
Then, in your index and show pages, you can access that instance variable like this:
views/device/show.rb:
<% #details.each do |detail| %>
<%= render detail %>
<% end %>
It should loop through each of your details and render each one. If you only want to show the the "no" of details it should look like this:
<% #details.each do |detail| %>
<%= detail.no %>
<% end %>
Or, if you're on the show page you could simply call the detail for that page like this:
<%= detail.no %>
Is this what you were asking?
Related
In my controller I have:
#payment_types = [['Cash' ,'1'], ['Card', '2']]
What I'm trying to achieve is to show in view Cash and Card while writing on database 1 and 2.
In my view I tried:
<% payment_types.each do |payment_type| %>
<%= payment_type %>
<% end %>
which shows ['Cash' ,'1'] ['Card', '2']]
How can I show instead in my view Cash or Card?
I'm not sure if I understand your question, but if you want to show only 'Cash', and 'Card', you can do it by passing another argument (responsible for hash value, I called it _ because it's a convention for unused arguments) to your block, like this:
<% payment_types.each do |payment_type, _| %>
<%= payment_type %>
<% end %>
You could also do it like this
<% payment_types.each do |payment_type| %>
<%= payment_type.first %>
<% end %>
My code:
<% #pg.items.each do |i|%>
<%= i.item_name %> # will display item name
<%= i.price_group_lines.where(price_group_id: #pg.id).take %>
<% end %>
In browser I see #PriceGroupLine:0x007fcc05e53598.
How do i get a value?
Here you are trying to display an entire row from the db:
<%= i.price_group_lines.where(price_group_id: #pg.id).take %>
But you probably want to show a value from the row, like:
<%= i.price_group_lines.where(price_group_id: #pg.id).take.amount %>
That will show the amount (if that attribute exists) from the price_group_line.
I have the following loop
<% #sub_categories.dropdown_heads.each do |label| %>
<label for="login"><%= label.head_name %></label>
<%= select_tag "post[dynam][#{label.head_name}]", options_for_select(generate_option(label)) %>
<% end %>
Here is my helper function used for generating dropdown
def generate_option opt
opt.dropdown_lists.inject([]) do |memo, cat|
memo << [cat.list_name, cat.list_name]
end
end
The above piece of code will generate the following result(check the screen shot).
In my database table I have the column called content which holds similar datas
{"Style":"convertible","Year":"2010","Color":"green"}
Since the above code is from edit form, I need to show the selected values in select dropdown How can i parse the json data and show the chosen value any suggestion please.
Edit 1
I changed the line to
<%= select_tag "post[dynam][#{label.head_name}]", options_for_select(generate_option(label)), selected: get_value_for(label.head_name, #post) %>
It is not showing error but it is not filtering just displaying normally
My helper
def get_value_for(head_name, post)
content_hash = JSON.parse post.content
content_hash[head_name]
end
Have another helper to get the value for the label.
<% #sub_categories.dropdown_heads.each do |label| %>
<label for="login"><%= label.head_name %></label>
<%= select_tag "post[dynam][#{label.head_name}]", options_for_select(generate_option(label), get_value_for(label.head_name, #post)) %>
<% end %>
def get_value_for(head_name, post)
content_hash = ActiveSupport::JSON.decode(post.content)
content_hash[head_name]
end
I am assuming that you are setting #post in your controller and Post model is the one you are editing and it has the field content
Also this code is not tested. It should anyways give you the idea
I am trying to create a compare functionality for an index of schools. Currently I am using the following code which takes any checked school and adds it to the school_ids[] in the params.
In my gemfile:
gem 'will_paginate'
In my school's index.html.erb:
<%= form_tag compare_path, :method => 'get' do %>
<%= submit_tag "Compare" %>
<ul>
<% #schools.each do |school| %>
<li>
<%= check_box_tag'school_ids[]', school.id %>
<%= link_to school.name, school %><br>
<%= school.city %>, <%= school.state %>
</li>
<% end %>
</ul>
<% end %>
In my school controller I have:
def compare
#schools = School.find(params[:school_ids])
end
This works great as long as all of the check schools are on the same page. But since I'm using will_paginate to paginate the list of schools, if I change pages, the check boxes do not persist. I'm assuming I need to save to sessions somehow.
Do you mean you want to be able to add a check mark to a school A on page 1 of the index, go to page 2 of the index and add another check mark for school B, then submit the compare form and see schools A and B? If that's the case, then you're correct, you need to get the check boxes into the session. Attach a js click event, like
$('.checkbox_class').click(function(){
$.post('update_session_method', { school_id: $(this).val(), checked: $(this).is(:checked)]);
});
then add a controller method
def update_session_method
session[:school_ids] ||= []
if params[:checked]
session[:school_ids] << params[:school_id]
else
session[:school_ids].delete(params[:school_id])
end
end
then your compare method
def compare
#schools = School.find(params[:school_ids].merge(session[:school_ids] || []))
end
I have an action in the controller:
def user_detail
#user_detail = UserDetail.find_by_id(11)
end
And in the view:
<%= #user_detail -%> // displays me like #
I am trying to retrieve the contents of #user_detail: actually the hash contains {:empid=>"11111", :prjtname=>"aaaaa", :prjtrole=>"Developer"}
How do I display the user detail's empid and other values?
Since I know what question you asked earlier, I think this is the syntax you actually want to use:
<%= #user_detail.additional_info[:empid] %>
Unless of course you renamed the name of the hash :)
Another approach, if you want all the content from the hash but the keys varies from each record, you could loop through them like this:
<% #user_detail.additional_info.each_pair do |key, value| %>
<p>Key: <%= key %> Value: <%= value %></p>
<% end %>
To get simple debug output like the example you posted, this will handle it:
<%= #user_detail.inspect %>
try this <%= #user_detail.emplid %> <%= #user_detail.prjtname %> <%= #user_detail.prjtr %>
More of an extraction from #dln's answer
try using
<%= #user_detail[:emplid] %>
<%= #user_detail[:prjtname] %>
<%= #user_detail[:prjtr] %>
Hope this solves your prob