我正在学习 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