How can I render partial in show.html.erb - ruby-on-rails-3.1

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 %>

Related

How to pull a specific field from a Ruby Associations CollectionProxy?

I have the following models:
class User < ApplicationRecord
has_many :hobbies, dependent: :destroy
end
class Hobby < ApplicationRecord
belongs_to :user
end
I was trying to output to my view the user and its hobbies:
<table>
<tr>
<th>User</th>
<th>Hobby</th>
</tr>
<% #users.each do |u| %>
<tr>
<td><%= u.name %></td>
<td><%= u.hobbies %></td>
</tr>
<% end %>
</table>
But I see the following:
User Hobby
Mad Max <Hobby::ActiveRecord_Associations_CollectionProxy:0x007fa2721ab910>
It looks like I am outputting the collection, but I want the actual hobby name. I have tried u.hobbies.first, u.hobbies[:hobby], etc. I've looked inside http://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html but it's abit confusing for me as a new developer. Can someone tell me how to pull from a specific field from the collection? And which resource out there is a good guide for me to reference in the future?
So this is basically an array of hobbies that gets attached to the user. so you need to iterate over each one and then ask for the attribute you want from it. Try something like this:
<table>
<tr>
<th>User</th>
<th>Hobby</th>
</tr>
<% #users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td>
<% user.hobbies.each do |hobby| %>
<%= hobby.attribute_name %>,
<% end %>
</td>
</tr>
<% end %>
</table>
If you want to get real fancy you could try doing something like this:
<% user.hobbies.each_with_index do |hobby, index| %>
<%= hobby.attribute_name %>
<%= (index == user.hobbies.length-1) ? '' : ','
<% end %>
That is using a ternary operator to basically say only put a comma if the hobby is not the last hobby

ActionMailer looping through nested attributes in Rails

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

How do I create a table to display all the instances of my Ruby class using ERB?

My table has the following columns:
<tr>
<th>Number</th>
<th>User</th>
<th>Password</th>
<th>UID</th>
<th>GID</th>
<th>GCOS</th>
<th>Home Directory</th>
<th>Login Shell</th>
</tr>
I have class Student, which looks like this:
class Student
attr_accessor :user_name, :password, :uid, :gid,
:gcos_field, :directory, :shell
attr_reader :count
def initialize(data)
#user_name,#password,#uid,#gid,#gcos_field,#directory,#shell = data
##count = defined?(##count) ? ##count += 1 : 1
end
end
There are about thirty instances of the class under the array map:
#students = datalines.map { |line| Student.new(line.chomp.split(":")) }
A sample element from datalines array is this:
dputnam:x:4185:208:Douglas Vernon Putnam:/users/dputnam:/bin/bash
I haven't been able to get the ##count to function properly either.
My failed attempt at producing a table made all the student names (of my Ruby class incidentally) display horizontally rather than vertically:
<% #students.each do |parameters| %>
<td><%= parameters.user_name %></td>
<% end %>
Any help would be appreciated!
Here's a link to the output if it helps: http://hills.ccsf.edu/~wly3/cs132a/lab4.cgi
Okay the ERB template (I think this is what it is) since it was requested:
<h1>CS132A Lab 4</h1>
<br>
<br>
<table>
<tr>
<th>Number</th>
<th>User</th>
<th>Password</th>
<th>UID</th>
<th>GID</th>
<th>GCOS</th>
<th>Home Directory</th>
<th>Login Shell</th>
</tr>
<tr>
<% #students.each do |parameters| %>
<td><%= parameters.user_name %></td>
<% end %>
</tr>
</table>
<% finish = Time.now %>
<h2>Elapsed time in seconds:<%= (finish.to_f - start.to_f).to_s %></h2>
You have to generate a new <tr> for each student:
<% #students.each do |parameters| %>
<tr>
<td><%= parameters.user_name %></td>
</tr>
<% end %>

Rails 3 : Mass update

I want to use mass-update every operation in a single view, with a single update button. Using this following code, Rails thows this error
Showing /home/vincent/git/gestion/app/views/operations/tag.html.erb where line #23 raised:
undefined method `merge' for 1:Fixnum
Extracted source (around line #23):
20: <td>
21: <% #tags.each do |elem| %>
22: <%= f.label elem.tag %>
23: <%= f.check_box "operation[tag_ids][]", elem.id, operation.tags.include?(elem) %>
24: <% end %>
25: </td>
26: <td><%= f.submit %></td>
Models
class Operation < ActiveRecord::Base
attr_accessible :credit, :date_operation, :debit, :libelle, :tag_ids
has_and_belongs_to_many :tags
accepts_nested_attributes_for :tags, :allow_destroy=>true
end
class Tag < ActiveRecord::Base
attr_accessible :id, :tag
has_and_belongs_to_many :operations
end
Controller
def tag
#operations = Operation.limit(100)
#tags = Tag.all
respond_to do |format|
format.html { "tag" }# tag.html.erb
# format.json { render json: #operations }
end
end
View
<% #operations.each do |operation| %>
<tr>
<td><%= operation.date_operation %></td>
<td><%= operation.libelle %></td>
<td><%= operation.credit %></td>
<td><%= operation.debit %></td>
<%= form_for operation do |f| %>
<td>
<% #tags.each do |elem| %>
<%= f.label elem.tag %>
<%= f.check_box "operation[tag_ids][]", elem.id, operation.tags.include?(elem) %>
<% end %>
</td>
<td><%= f.submit %></td>
<% end %>
</tr>
<% end %>
Do you have any clue/help about this problem?
Thank you in advance
Edit 1 : adding full stack trace
You need to change the f.check_box for check_box_tag, for instance:
<%= check_box_tag "operation[tag_ids][]", elem.id, operation.tags.include?(elem) %>
The problem in this scenario is that f.check_box is expecting that the value is bounded to the form which in not in this case.
use nested_form gem for this case i think it will work.
for more about nested_form Click here

Ajax : data retrieving in rails3 + formtastic

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

Resources