My loop:
<% #products.first.attributes.except('name', 'created_at','updated_at','id').each do |attr_name, attr_value| %>
<tr>
<td><span><%= t(attr_name) %></span></td>
<td class="middle">Pr. x</td>
<%= #products.each do |f| %>
<td class="last"><%= f.attr_name %> ,-</td>
<% end %>
<tr>
<% end %>
Then I get this error: undefined method attr_name for #<Product:0x3adc850
How do I use the column method with attr_name? I have tried things like f."#{attr_name}" without luck.
Use send
<%= #products.each do |f| %>
<td class="last"><%= f.send attr_name %> ,-</td>
<% end %>
And you should rename f into product (f is generally used for form builders)
Related
How can I loop through the following output code from the console? Essentially, stories is a nested attribute of an entry and I need to pass "story.content" to a message variable that is used in the email template.
//output in firefox console from new.html.erb
name="message[story_attributes][7][content]"
//new.html.erb This is the form
<% for story in #entry.stories %>
<%= fields_for "message[story_attributes][]", story do |ff| %>
<tr>
<th><%= ff.label(:content) %></th>
<td><%= ff.text_area(:content) %></td>
</tr>
<% end %>
<% end %>
//internal_email.html.erb - ?????
<% for ... in #message.story_attributes %>
?????
<% end %>
If the app is not sending the email, try changing deliver to deliver!
also, make sure that you reinitialize the values in InternalEmail class
class InternalEmail << ActionMailer::Base
def internal_email(message)
#message = message
mail(to: "", subject: "")
end
end
I am trying to read a simple CSV file into an HTML table to be displayed in a browser, but I'm running into trouble. This is what I'm trying:
Controller:
def show
#csv = CSV.open("file.csv", :headers => true)
end
View:
<% #csv.read %>
<% #csv.headers.each do |head| %>
<%= head %>
<% end %>
<table border="1">
<% #csv.each do |row| %>
<tr>
<% row.each do |element| %>
<td> <%= element %> </td>
<% end %>
</tr>
<% end %>
</table>
Output:
Name Start Date End Date Quantity Postal Code
Basically I am only getting the header, and the CSV body isn't being read and rendered.
This ended up being the final solution:
Controller:
def show
# Open a CSV file, and then read it into a CSV::Table object for data manipulation
#csv_table = CSV.open("file.csv", :headers => true).read
end
View:
<table border="0" cellspacing="5" cellpadding="5">
<tr>
<% #csv_table.headers.each do |header| %>
<th><%= header %></th>
<% end %>
</tr>
<% #csv_table.each do |row| %>
<tr>
<% row.each do |element| %>
<td><%= element[1] %></td>
<% end %>
</tr>
<% end %>
</table>
Reading the CSV into a table was the correct way to go about it, but when iterating through the rows, I had to specify element[1], since element is actually an array of [header, element].
I was able to do the same but didn't need to use the ".read"
def show
# Open a CSV file, and then read it into a CSV::Table object for data manipulation
#csv_table = CSV.open("file.csv", :headers => true)
end
and using the key/value for the elements.
<% #csv_table.each do |row| %>
<tr>
<% row.each do |key, value| %>
<td><%= value %></td>
<% end %>
</tr>
<% end %>
I have these two models in my Ruby on Rails application - Artist and Song. They are associated as follows:
class Artist < ActiveRecord::Base
has_many :songs
attr_accessible :artist_name
and
class Song < ActiveRecord::Base
belongs_to :artist
attr_accessible :title, :track_URL, :artist_id
I have this code in views/artist/show.html.erb:
<%= render 'artist_song' %>
<table>
<% #artist.songs.each do |song| %>
<tr>
<td><%= song.title %></td>
</tr>
<% end %>
</table>
The partial Im trying to render(_artist_song.html.erb) in the same view looks like this:
<table>
<% #artist = Artist.all %>
<% #artist.each do |artist| %>
<tr>
<td><%= link_to artist.artist_name, artist %></td>
</tr>
<% end %>
</table>
The way it is suppose to work is when I click on an artist shown trough the partial, the code below the partial has to show me all the songs that belongs to the particular artist.
Both the partial and the code in the table tag are working individually. But when I put them together, it looks like there is a conflict between them and the server is showing me this No Method Error:
NoMethodError in Artists#show
Showing C:/Sites/OML/app/views/artists/show.html.erb where line #9 raised:
undefined method `songs' for #<Array:0x5fe1418>
Extracted source (around line #9):
6:
7:
8: <table>
9: <% #artist.songs.each do |song| %>
10: <tr>
11: <td><%= song.title %></td>
12: </tr>
Rails.root: C:/Sites/OML
Application Trace | Framework Trace | Full Trace
app/views/artists/show.html.erb:9:in `_app_views_artists_show_html_erb__950110288_54062208'
app/controllers/artists_controller.rb:21:in `show'
I couldn`t find a solution. Any help will be appreciated.
Thank you.
You are not using a partial. You need to render the partial inside the artist loop:
show.html.erb
<table>
<% #artists = Artist.all %>
<% #artists.each do |artist| %>
<tr>
<td><%= link_to artist.artist_name, artist %></td>
</tr>
<%= render :partial => 'artist_song', :artist => artist %>
<% end %>
</table>
This way, you are passing the current artist object to inside the partial, so there you can do:
artist_song.html.erb
<% artist.songs.each do |song| %>
<tr>
<td><%= song.title %></td>
</tr>
<% end %>
I need to replace X_VARIABLE in 2 places.
For the first X_VARIABLE I want to keep the text 'remove'
For the second X_VARIABLE I want to keep 'd_cars_path'
<% #cars.each do |x| %>
<% #a = #b.send(x) %>
<% if #a == true %>
<%= button_to "removeX_VARIABLE", X_VARIABLEd_cars_path(:id => #user.id), class: "btn btn-large btn-primary" %>
<% end %>
<% end %>
I am looking for some help with the variable substitution syntax. Thanks.
I'd write:
<% #cars.each do |x| %>
<% if #b.send(x) %>
<%= button_to "remove#{x}",
send(:"#{x}d_cars_path", id: #user.id),
class: "btn btn-large btn-primary" %>
<% end %>
<% end %>
I have simple form:
<%= form_tag('/paymentShops/newww') do -%>
<table class="adm_prehled"><tr>
<% i = 0%>
<% for dopr in #settings %>
<% i += 1 %>
<td class="posl_import">
<%= check_box_tag 'settings['+dopr.id.to_s+']', dopr.id%>
<%= dopr.value %>
</td>
<% if i == 4%>
</tr><tr>
<% i = 0%>
<%end%>
<% end %>
</tr> </table>
<div><%= submit_tag('Sent') %></div>
<% end -%>
and in controller
def newww
params[:settings].each do |page|
puts page
end
end
and in terminal I am getting always the statement from puts twice... e.g.: I will select the inputs with numbers 3, 5, 9, co I will get in terminal:
33
55
99
Could anyone help, me, please, what is wrong?
Thank you
The solution:
<%= check_box_tag 'settings[]', dopr.id%>