我想在我的jokes#index
页面上显示该笑话文本下Joke
作者的名字,但是我做错了什么。 A User
has_many
jokes
和joke
belongs_to
a User
。显示资源在索引页上的作者/所有者(Rails)
下面是我的jokes#index
再培训局:
<% @jokes.each do |joke| %>
<h2 id="joke-title" style="margin-top: 50px">
<%= joke.title %>
<% if joke.kids == true %>
<%= image_tag 'icon_kids_green.jpg', style: "height: 25px; margin-left: 10px" %>
<% end %>
<% if joke.mixed == true %>
<%= image_tag 'icon_mixed_purple.jpg', style: "height: 25px; margin-left: 10px" %>
<% end %>
</h2>
<p id="joke-body"><%= joke.body %></p>
<p><strong>Submitted by: <%= User.find(joke.user).first_name %></strong></p>
<% if current_user.admin && joke.approved == nil && joke.rejected == nil %>
<%#= link_to do %>
<span class="glyphicon glyphicon-ok" aria-hidden="true"></span>Approve Joke
<%# end %>
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>Reject Joke
<% end %>
<% if current_user == joke.user || current_user.admin %>
<%= link_to edit_joke_path(joke) do %>
<span style="color: blue" class="glyphicon glyphicon-pencil" aria-hidden="true"></span><span style="color: blue">Edit Joke</span>
<% end %>
<%= link_to joke_path(joke), data: {:confirm => 'Are you sure?'}, :method => :delete do %>
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>Delete Joke
<% end %>
<% end %>
<% end %>
这里是我的jokes_controller
索引定义:
def index
@jokes = Joke.all
end
我觉得这应该是一个相当简单的问,所以我必须做的事情非常愚蠢。我得到的错误信息是:
Couldn't find User with 'id'=
这个错误被称为在我erb
线来回询问用户名。
看来你的'joke.user_id'是零。你能提供一下'joke.inspect'的输出吗? – retgoat
@retgoat,'joke.inspect'产生这样的结果:''#<笑话id:4,标题:\“计算牧羊犬\”,身体:\“在一个说话的牧羊犬得到了所有的绵羊...... \ ,孩子:真,混:真,批:无,被拒绝:无,user_id:无,created_at:\“2016-07-31 23:18:15 \”,updated_at:\“2016-07-31 23:18 :15 \“>”' – Liz