【问题标题】:Showing username with association with Rails显示与 Rails 关联的用户名
【发布时间】:2015-07-23 21:24:12
【问题描述】:

我一直在研究,但似乎找不到答案。我要做的就是为用户和文章设置关联(最基本的设置=P)。无论如何,我在模型中设置了 has_many 和 belongs_to,将 user_id 添加到文章模型中。在我的创建操作中,我添加了一个变量来保存 current_user,然后我可以让它出现在显示页面中。问题是我不能让用户名出现在索引页面中(在我的情况下,它被称为社区)。以下是各种代码:

Article.rb(模型):

class Article < ActiveRecord::Base
belongs_to :user
...

User.rb(模型):

class User < ActiveRecord::Base
has_many :articles
...

articles_controller:

def create 
@article = Article.create(article_params)
@article.user = current_user
if @article.save
    flash.notice = "Article #{@article.title} has created!"
    redirect_to community_path
  else
    flash.notice = "Try Again!"
  end

结束

显示视图:

....
<p><%= @article.user.first_name %> <%= @article.user.last_name %></p>
....

社区(索引)视图:

<div class="center_com_col col">
<div class="main_community">
<% @articles.each do |x| %>
    <ul>
        <li>
            <h2><%= link_to article_path(x) do %><%= x.title %><% end %><br>
            <p>
                <%= Code to go here for user =><br>
                Written on: <%= x.created_at.strftime("%B %d, %Y") %>
            </p>
            </h2>
        </li>
        <li>
            <p><%= truncate simple_format(x.body), length: 250, escape: false  %> (<%= link_to "read more", article_path(x) %>)</p>
        </li>
    </ul>
<% end %>
</div>

一切似乎都运行良好,但由于我正在遍历文章,因此我对那个社区(索引)页面一无所知。

非常感谢您提供的任何帮助!

编辑:这是我的节目和社区管理员:

def community
  @articles = Article.all.order("created_at DESC").limit(5)
  @users = User.all.order("created_at DESC")
  render :layout => 'community_layout'
end

def show
  @article = Article.friendly.find(params[:id])
  @users = User.all.order("created_at DESC")
  render :layout => 'community_layout'
end

【问题讨论】:

  • &lt;%= Code to go here for user =&gt; 应该是&lt;%= x.user.first_name %&gt; &lt;%= x.user.last_name %&gt;
  • 我试过了,但它会导致这个错误:未定义的方法 `first_name' for nil:NilClass
  • 这意味着您有一篇没有作者的文章。 :) 两个选项:选项 1)这不应该发生!为您的文章添加验证以检查用户的存在并修复有问题的文章。选项 2) 是允许的。在显示用户之前添加if x.user
  • 这很奇怪 - 节目显示作者没有问题,但索引有问题...我想我的创建控制器可能有问题?这条线@article.user = current_user 是什么让这个名字出现在节目中,所以这可能是不正确的使用?此外,当我进入控制台并检查时,文章中列出了正确的 user_id。
  • 问题是您的一篇文章没有作者。访问显示页面时,您只显示一篇文章,很可能有作者。循环时,您正在显示所有文章 - 其中一篇没有作者就足以引发此错误。

标签: ruby-on-rails ruby loops ruby-on-rails-4 associations


【解决方案1】:

我同意 BroiSatse。

遵循一些最佳实践可以防止这些错误:

  • 除非引用 x 位置,否则不要使用 x 作为变量名!

  • 如果@articles.any 总是这样做?如果没有,在开始之前 会导致 Nil class Nil 之类错误的文章!

  • 在使用 art.user 之前,请先确定是否存在 art.user。

这里应该是代码:

<ul> <!--The UL should not be part of your each loop. -->
<% if @articles.any? %>
  <% @articles.each do |art| %>
    <li>
      <h2><%= link_to article_path(art) do %><%= art.title %><% end %><br>
        <p>
    <% if art.user %>
       <%= art.user.first_name %> <%= art.user.last_name %><br>
    <$ end %></h2>
    <%= time_ago_in_words(art.created_at) %>
       </p>
   </li>
   <li>
     <p>
     <%= truncate simple_format(x.body), length: 250, escape: false  %> (<%= link_to "read more", article_path(x) %>)
     </p>
   </li>
<% end %>  <!--end refers to @articles.each -->
</ul> 
 <% end %> 
   <!--end refers to @articles.any? -->

我相信您的某些文章可能不是由创作它们的用户创建的,请确保您的

db/seeds.rb:

user = User.create(email: "email@gmail.com", first_name: Faker.first_name, last_name: Faker.last_name)
user.articles.create(title: "ARTICLEs are awesome man!", body: 'Yes in a shocking report people are reading articles")

还要专门测试一下:

rails c

进入rails控制台。

articles = Article.all
articles.each { |art| print art.user } => I am guessing you'll get nilClass Nil!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多