简体   繁体   中英

Rails association works in the console but not in the view

to-many association between 2 modles. It works perfectly in the console but in the view just I get object-references appears like this:

#<Author:0x0000000434bf80>
#<Author:0x000000043485b0>

This appears in my view which has this code:

<h1 class="page-title">Articles</h1>
<hr>

<div class="category-container">
    <ul  class="category-titles">
      <% @cat.each do |c| %> 
       <li><%= link_to c.catName, category_path(c) %></li>
      <% end %>
  </ul>
</div>
<br><br><br><hr>

<% @art.each do |t| %>
 <p class="articles-list-page"><%= link_to t.artTitle, article_path(t)  %></p>
 <p><%= t.author %></p>
<% end %>

Here is my association in Author Model

class Author < ActiveRecord::Base
 has_many :articles
end

and Here is my association in Article Model

 class Article < ActiveRecord::Base
    belongs_to :category
    belongs_to :author
 end

I could not understand why it is working well in the console but not in the view

It works fine in the view.

This line:

<p><%= t.author %></p>

outputs the author model. What you probably want to do is output the author name - something like

<p><%= t.author.name %></p>

You're attempting to output an ActiveRecord relation to the view. There's probably no situation ever where you'd want to display an entire ActiveRecord object in a view. Instead, you'd want to display particular attributes of the object.

Such as:

t.author.created_at
t.author.name
t.author.whatever

However, if there was some strange reason you wanted to output the entire object to the view, you could use inspect like so:

t.author.inspect

UPDATE:

To answer the other issue you're running into, you'll need to make sure that you actually have a related Author for each of the Articles before trying to output an Author attribute to the view. You can accomplish that like so:

<% if t.author.present? %>
  <p><%= t.author.authName %></p>
<% else %>
  <p>No author available</p>
<% end %>

Or like so, if you want to use a terniary operator to keep things on one line:

<p><%= t.author.present? ? t.author.authName : 'No author available' %></p>

Or if you don't care about returning a default value such as "No author available" if an author isn't available, then you could just do something like this:

<p><%= t.author.try(:authName) %></p>

You should delegate that author attributes to Article model

class Article < ActiveRecord::Base
  belongs_to :category
  belongs_to :author
  delegates :authName, allow_nil: true
end

Also in your controller use following code

class ArticleController < ApplicationController
  def index
    @art = Article.includes(:author).all
  end
end

And in your view use like bellow

<% @art.each do |t| %>
 <p class="articles-list-page"><%= link_to t.artTitle, article_path(t)  %></p>
 <p><%= t.authName %></p>
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM