0

我正在学习 Skillshare.com 上的一个月 Rails 课程,但遇到了一个我自己(还)无法解决的问题。

我正在制作一个类似于 pinterest 的简单应用程序。我正处于一个步骤,我们试图在他们发布图片后在他们的图片下获取用户名。它将显示“发布者:(姓名)”。

这是我的代码有错误:

<div class="box">
<%= link_to (image_tag pin.image(:medium)), pin %>
<p class="description">
    <%= pin.description %>
</p>
<p>
    <strong>
        Posted by: <%= pin.user.name %>
    </strong>
</p>
<% if current_user == pin.user %>
    <p>
      <%= link_to 'Edit', edit_pin_path(pin) %>
      <%= link_to 'Destroy', pin, method: :delete, data: { confirm: 'Are you sure?' } %>
    </p>
<% end %>

在强标签之间的中心,我有“发布者:<%= pin.user.name %>”。我尝试取出 .name 并且我的网站将在没有“未定义方法名称”的情况下加载,但我得到了一堆古怪的数字(我猜这是 ruby​​ 或 rails 将用户定义为,然后将名称分配给那个正在显示的一批数字)。

我的猜测是我需要在某个地方定义 .name ,我不太确定在哪里或如何做到这一点。如果有人可以看一下,我将不胜感激。谢谢!

**我刚刚在我的 show.html.erb 文件中找到了这段代码

<div class"row">
    <div class="span6 offset3">
        <div class="well">
            <%= image_tag @pin.image %>
            <%= @pin.description %>
            <p>
              <%= @pin.user.name %>
            </p>
            <% if current_user == @pin.user %>
            <%= link_to 'Edit', edit_pin_path(@pin) %> 
            <% end %>|
            <%= link_to 'Back', pins_path %>
        </div>
    </div>
</div>

pin.user.name 在这种情况下有效,但我仍然无法让它在上面的第一段代码中工作。

模型/用户.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :rememberable, :trackable, :validatable #recoverable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :name
  # attr_accessible :title, :body

  has_many :pins, :dependent => :destroy
end
4

3 回答 3

0

我猜你有一些pin记录,没有user_id. rails c您应该使用带有以下语句的 rails 控制台 ( ) 来检查这一点:

Pin.where(:user_id => nil)

在你的用户模型中添加类似的东西validates :user_id, :presence => true,以确保曾经pin有一个相应的用户。

于 2013-06-07T19:21:49.923 回答
0

我也在上这个课。有另一个问题,但通过阅读课堂材料,有一些明确的步骤可以纠正:

故障排除注意:您可能应该删除任何没有与之关联的用户的 pin,否则当您尝试加载 pin 索引页面时,您将得到一个未定义的方法 'name' for nil:NilClass 错误。按着这些次序:

$ rails console
>> Pin.destroy_all(user_id: nil)
$ heroku run rails console
>> Pin.destroy_all(user_id: nil)
于 2013-09-18T01:49:36.317 回答
0

好的,所以我们已经确定这应该是部分的。在这种情况下,您需要传入form_for正在渲染的变量。我可能不正确,但我相信您的身份如下。这会在模型对象周围创建一个表单和一个范围。因此,在这种情况下,我相信您将拥有以下内容:

   <%= form_for(@pin) do |f| %>
    <div class="box">
    <%= link_to (image_tag pin.image(:medium)), pin %>
    <p class="description">
        <%= pin.description %>
    </p>
    <p>
        <strong>
            Posted by: <%= pin.user.name %>
        </strong>
        <% end %> 
    </p>
    <% if current_user == pin.user %>
        <p>
          <%= link_to 'Edit', edit_pin_path(pin) %>
          <%= link_to 'Destroy', pin, method: :delete, data: 
                                     { confirm: 'Are you sure?' } %>
        </p>
    <% end %> 
  <% end %> 

您可能会受益于阅读Ruby on Rails 指南 - 使用 Partials

于 2013-06-07T19:43:53.820 回答