Yosuke Suzuki
2006-Jun-25 01:53 UTC
[Rails] how to write the codes in view?(newbie question)
Hi.
I have two tables and want to join them.
<genres>
id
genre_name
genre_order
<links>
id
genre_id
name
symbol
url
order
display
etc
The code of links_Controller is like below.
def list
@links = Genre.find(:all,:include => :link)
end
Below is the mysql log.
SELECT genres.`id` AS t0_r0, genres.`genre_n
ame` AS t0_r1, genres.`genre_order` AS t0_r2, links.`id` AS t1_r0,
links.`genre_
id` AS t1_r1, links.`name` AS t1_r2, links.`symbol` AS t1_r3,
links.`url` AS t1_
r4, links.`order` AS t1_r5, links.`display` AS t1_r6, links.`etc` AS
t1_r7 FROM
genres LEFT OUTER JOIN links ON links.genre_id = genres.id
But I don''t know the way to show them in the view.
How do I have to write the code in the view(list.rhtml)
to get the contents(name,symbol,url etc) of the joined table?
list.rhtml
<% for link in @links %>
<%= link[:genre_name] %><br/> <- works fine
<%= link[:name] %><br/> <- just blank
<%= link[:url] %><br/> <- just blank
<%= link[:order] %><br/> <- just blank
<% end %>
Jonathan Viney
2006-Jun-25 02:26 UTC
[Rails] how to write the codes in view?(newbie question)
I suggest you find a Rails tutorial and work through it. It doesn''t seem like you''ve got the basics yet. Your controller and view code don''t match up. You''re selecting genres in the controller and then trying to display a bunch of links in the view. @links = Link.find(:all, :include => :genre) may be what you need in the controller. Also, don''t use [] to get values out of AR objects, use the reader method (eg: link.name instead of link[:name]. -Jonathan.> > The code of links_Controller is like below. > def list > @links = Genre.find(:all,:include => :link) > end > > Below is the mysql log. > SELECT genres.`id` AS t0_r0, genres.`genre_n > ame` AS t0_r1, genres.`genre_order` AS t0_r2, links.`id` AS t1_r0, > links.`genre_ > id` AS t1_r1, links.`name` AS t1_r2, links.`symbol` AS t1_r3, > links.`url` AS t1_ > r4, links.`order` AS t1_r5, links.`display` AS t1_r6, links.`etc` AS > t1_r7 FROM > genres LEFT OUTER JOIN links ON links.genre_id = genres.id > > But I don''t know the way to show them in the view. > > How do I have to write the code in the view(list.rhtml) > to get the contents(name,symbol,url etc) of the joined table? > > list.rhtml > <% for link in @links %> > <%= link[:genre_name] %><br/> <- works fine > <%= link[:name] %><br/> <- just blank > <%= link[:url] %><br/> <- just blank > <%= link[:order] %><br/> <- just blank > <% end %> > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Yosuke Suzuki
2006-Jun-25 16:03 UTC
[Rails] how to write the codes in view?(newbie question)
Hi, Jonathan. Thank you for your reply. I managed to get the result which I wanted. I have the agile book, so I try the tutorial from the begining. Yosuke Suzuki Jonathan Viney wrote:> I suggest you find a Rails tutorial and work through it. It doesn''t > seem like you''ve got the basics yet. > > Your controller and view code don''t match up. You''re selecting genres > in the controller and then trying to display a bunch of links in the > view. @links = Link.find(:all, :include => :genre) may be what you > need in the controller. > > Also, don''t use [] to get values out of AR objects, use the reader > method (eg: link.name instead of link[:name]. > > -Jonathan. > >> >> The code of links_Controller is like below. >> def list >> @links = Genre.find(:all,:include => :link) >> end >> >> Below is the mysql log. >> SELECT genres.`id` AS t0_r0, genres.`genre_n >> ame` AS t0_r1, genres.`genre_order` AS t0_r2, links.`id` AS t1_r0, >> links.`genre_ >> id` AS t1_r1, links.`name` AS t1_r2, links.`symbol` AS t1_r3, >> links.`url` AS t1_ >> r4, links.`order` AS t1_r5, links.`display` AS t1_r6, links.`etc` AS >> t1_r7 FROM >> genres LEFT OUTER JOIN links ON links.genre_id = genres.id >> >> But I don''t know the way to show them in the view. >> >> How do I have to write the code in the view(list.rhtml) >> to get the contents(name,symbol,url etc) of the joined table? >> >> list.rhtml >> <% for link in @links %> >> <%= link[:genre_name] %><br/> <- works fine >> <%= link[:name] %><br/> <- just blank >> <%= link[:url] %><br/> <- just blank >> <%= link[:order] %><br/> <- just blank >> <% end %> >> >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >