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.
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 %>
I want to display a checkbox for all elements in a model called MyModel. Here is what I wrote:
<%= MyModel.all.each do |c| %>
<%= check_box_tag(:id) %>
<%= label_tag(:name, c[:name]) %><br>
<% end %>
It does show the checkboxes as expected but at the end, I also get a list of the model content as shown in this screenshot.
Actually, it seems to be related to <%= MyModel.all.each do |c| %> because just printing out simple text still prints the whole model table content at the end:
<%= MyModel.all.each do |c| %>
toto<br>
<% end %>
shows this screenshot
Any idea how to get rid of this list at the end?
Thanks!
<%- MyModel.all.each do |c| %>
<%= check_box_tag(:id) %>
<%= label_tag(:name, c[:name]) %><br>
<% end %>
- - evaluates code.
= - evaluates code and outputs.
I am trying to create a checklist in rails using form_for. This checklist is taken from a table which I gained in the create action of my sign_ins controller:
#content = OrientationContent.where(site_id: session[:site_id])
In my view I want to use the form_for helper to iterate through the list in #content:
<%= form_for(:sign_ups ) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>
However this is not working and it produces two square brackets on the page: [].
How do I go through the list and print the name while creating a check box on the left of it? The check box does not have any meaning or data, I just need it present for reference.
Solved:
In the controller, need to pluck an individual field:
#content = OrientationContent.where(site_id: 1).pluck(:content)
In the view, structure as so:
<%= form_for(:sign_ups) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>
How do I change all count value at same time, currently i have to change each of them individually.
<% #book.each do |book| %>
<%= form_for(book) do |f| %>
<%= f.number_field :count %>
<% end %>
<% end %>
lets say my current value 2,5,1 and when i change :count field to 3, result should be like 3,3,3 to all. Thanks
You need to be more specific.
if you need to update a column for all rows use this
Book.update_all(:count, 3)
or on book controller
Book.update_all(:count, params[:book][:count])
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?