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
Related
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 %>
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)
I have successfully tried ajax saving in my sample formtastic with ajax form.
The new value is added to the database. But the the problem is in retrieving the list from the database as soon as i save via ajax.
How to do it.?
As soon as I add a new record I want my displaying list to be update. Both the option to add new record and list the data from database is in same page
This is my Index page. The controller and all other created via scaffolding
<h1>Listing samples</h1>
<table>
<tr>
<th><%=t :Name%></th>
<th></th>
<th></th>
<th></th>
</tr>
<% #samples.each do |sample| %>
<tr>
<td><%= sample.name %></td>
<td><%= link_to 'Show', sample %></td>
<td><%= link_to 'Edit', edit_sample_path(sample) %></td>
<td><%= link_to 'Destroy', sample, :method => :delete, :data => { :confirm => 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Sample', new_sample_path %>
<br /><br /><br /><br /><br />
<%= semantic_form_for #sample1,:url => samples_path, :remote => true do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :input %>
<% end %>
<% end %>
When you save via ajax, you may have to change the way your controller responds, kinda like this:
def create
# stuff
respond_to do |format|
format.js
end
end
When you do this, rails would expect you to have created a file named create.js.erb, in which you can manipulate the data of your view(append new content to your table, render a whole new partial with your new object list, etc):
$('#your_list').html(
"<%= escape_javascript(render('your_table_partial')) %>"
);
Or
$('#your_list').append(
"<%= escape_javascript(render('your_item_of_the_table_partial')) %>"
);
These are just examples, i don't know your code enough to write the correct code for you, but you sure can use these as a base for your work.
I did the CRUD operation using these guidelines and it worked perfect
http://stjhimy.com/posts/07-creating-a-100-ajax-crud-using-rails-3-and-unobtrusive-javascript
I implemented destroy functionality using rjs template in rails. i got an error "XML Parsing Error: no element found" when destroy one record from database. is this right my coding?
I used the following versions ruby and rails:
Ruby version: 1.8.7
Rails Version: 2.3.8
Controller file:
def destroy
begin
#task = Task.find(params[:id])
#task.destroy
respond_to do |format|
#format.html { redirect_to(tasks_url) }
format.js
format.xml { head :ok }
end
rescue Exception => e
puts e
end
end
partial template file _task.html.erb:
<tr id="<%= dom_id(task) %>">
<td><%= task.name %></td>
<td><%= task.note %></td>
<td><%= task.priority %></td>
<td><%= link_to 'Show', task %></td>
<td><%= link_to 'Edit', edit_task_path(task) %></td>
<td>
<%= link_to 'Destroy', task, :confirm => 'Are you sure?', :method =>:delete,:remote => :true %>
index.rhtml file:
<div id='newform'>
<% form_for([#task, Task.new]) do |f| %>
<div>
<%= f.label 'Add a new task: ' %>
<%= f.text_field :name %>
</div>
<div>
<%= f.submit %>
</div>
<% end %>
</div>
<table id="alltasks">
<tr id="tablehead">
<th>Name</th>
<th>Note</th>
<th>Priority</th>
<th></th>
<th></th>
<th></th>
</tr>
<%= render #tasks %>
<!-- expanded: render :partial => "task", :collection => #tasks -->
</table>
<br />
<%= link_to 'New Task', new_task_path %>
destroy.js.rjs file:
page.alert('Hi')
The problem is that the xml format is getting requested from your destroy action, not the js format, and something is trying to parse the empty xml response produced by the format.xml { head :ok } line in the controller. It used to be that you could get away with the empty xml response, but it also looks like you're using unobtrusive javascript, and something somewhere in ujs tries to parse it.
One potential solution is to make a destroy.xml.builder view that produces a simple response, and change your controller action to read format.xml { render :layout => false } or something similar. You can also do some fun things with changing the data type your link is requesting, but I'd recommend avoiding that unless you really want to do something with your js response in destroy.js.rjs.