Hi. I'm creating news controller with relation to comment model. It's nearly the same as in akelos screencast. I've got news scaffold, with changed show.tpl, installed comment model, added news and comment with good news_id (in my example its news_id=1 ) News is displayed correctly, however, I haven't got any comments displayed... why?
comment model:
class Comment extends ActiveRecord
{
var $belongs_to = 'news';
}
news model:
class News extends ActiveRecord
{
var $has_many = 'comments';
}
show.tpl
<h2><%= link_to news.title, :action => 'show', :id =>news.id%> </h2>
<small>Dodano: {news.created_at} Przez: {news.created_by} </small> <br />
{news.summary} <br />
{news.body}
<h3>_{Komentarze}</h3>
{loop news.comments}
{comment.author}
{end}
<%= form_tag :action => 'comment', :id => news.id %>
<%= text_field 'comment', 'author' %><br />
<%= text_area 'comment', 'body'%> <br />
<%= submit_tag 'Komentuj!' %>
</form>
Have you tried {news.comment.author} instead of {comment.author}?
yes, it don't works...
Well, I've tried
{loop news.comments}
hello world?
{end}
and -I haven't got any output.
I would say most probably comments aren't loaded. =array()
well,
print_r($news->comments);
outputs empty array.
I've edited var $has_many into
var $has_many = array('comments');
with same result....
and you included or explicitly loaded the comments in your action or finder-method?
something like
...->find('all',array('include'=>'comments'));
or:
$News->comment->load();
well, I've made exactly like in screencast- there was no info about
$News->comment->load();
But after this- it works, thanks ;)
1 to 8 of 8