Hello! I'm new to rails ad ActiveRecord, and have a very basic
problem.
I have a very basic rails app, which I want to contain computers, and
notes. Each computer has associated notes. I'm getting an error I'm
not fully understanding when I try to view a computer. The view
should include a listing of notes for the associated computer, but
instead, I get this:
Couldn't find Note without an ID
The link that takes the user to the screen with the problem:
<%= link_to 'Show', :action => 'show', :id => computer %>
My controller:
def show
@computer = Computer.find(params[:id])
@notes = Note.find(params[:computer_id])
end
My view for show:
<% for column in Computer.content_columns %>
<p>
<b><%= column.human_name %>:</b> <%=h @computer.send(column.name) %>
</p>
<% end %>
<table>
<% for note in @notes %>
<tr>
<% for column in Note.content_columns %>
<td><%=h note.send(column.name) %></td>
<% end %>
</tr>
<% end %>
</table>
I realize I'm not setting :computer_id anywhere, but using :id in my
controller for notes gives me the same issue, and I don't want to pull
up notes with a particular id, but notes with a particular associated
computer. It seems like there should be a simple way to accomplish
this in rails, and I'm just missing it.
class Computer < ActiveRecord::Base
has_many :notes
end
class Note < ActiveRecord::Base
belongs_to :computer
end
What you probably want for the above is:
def show
@computer = Computer.find(params[:id], :include => :notes)
@notes = @computer.notes
end
Or if you wanted to search the notes without actually loading the Computer, and params[:id] included the computer id, you'd want:
def show
@notes = Note.find_by_computer_id(params[:id])
end
If you haven't read Agile Web Development With Ruby on Rails or one of the other rails books, it'd definitely be worth your while to do so, to get a better understanding of how rails handles parameters, and associations.
I have a very basic rails app, which I want to contain computers, and
notes. Each computer has associated notes. I'm getting an error I'm
not fully understanding when I try to view a computer. The view
should include a listing of notes for the associated computer, but
instead, I get this:
Couldn't find Note without an ID
The link that takes the user to the screen with the problem:
<%= link_to 'Show', :action => 'show', :id => computer %>
My controller:
def show
@computer = Computer.find(params[:id])
@notes = Note.find(params[:computer_id])
end
Presuming you have models along the lines of:
class Computer < ActiveRecord::Base
has_many :notes
end
class Note < ActiveRecord::Base
belongs_to :computer
end
What you probably want for the above is:
def show
@computer = Computer.find(params[:id], :include => :notes)
@notes = @computer.notes
end
Or if you wanted to search the notes without actually loading the
Computer, and params[:id] included the computer id, you'd want:
def show
@notes = Note.find_by_computer_id(params[:id])
end
If you haven't read Agile Web Development With Ruby on Rails or one
of the other rails books, it'd definitely be worth your while to do
so, to get a better understanding of how rails handles parameters,
and associations.
James.
small correction -
@notes = Note.find_by_computer_id(params[:id])
will return one row. Note.find_all_by_computer_id will do a find(:all
see my forthcoming announcement on find_by_associations to clean this up even more
Hello! I'm new to rails ad ActiveRecord, and have a very basic
problem.
I have a very basic rails app, which I want to contain computers, and
notes. Each computer has associated notes. I'm getting an error I'm
not fully understanding when I try to view a computer. The view
should include a listing of notes for the associated computer, but
instead, I get this:
Couldn't find Note without an ID
The link that takes the user to the screen with the problem:
<%= link_to 'Show', :action => 'show', :id => computer %>
I think you mean :id => @computer in the above line