所以我使用heroku在rails应用程序上提供我的ruby演示,并创建我的个人网站。但是使用live heroku应用程序,url articles / 2而不是获取帖子的ID,正试图获取user_id。所以说如果你有5篇文章,但有3个用户。然后你试着去找文章/ 5,它会在日志中给你一个错误。
5-12-04T10:03:51.370101 + 00:00 app [web.1]:在6ms内找不到404(ActiveRecord:4.3ms)
2015-12-04T10:03:51.361893 + 00:00 app [web.1]:开始GET “/ articles / 3”for 46.237.130.242 at 2015-12-04 10:03:51 +0000
2015-12-04T10:03:51.513724 + 00:00 app [web.1]:参数:{“id”=>“3”} 2015-12-04T10:03:51.513671 + 00:00 app [web.1]:处理方式 ArticlesController #show as HTML 2015-12-04T10:03:51.516207 + 00:00 app [web.1]:用户加载(1.6ms)SELECT“users”。* FROM“users”WHERE “users”。“id”= $ 1 LIMIT 1 [[“id”,3]]
2015-12-04T10:03:51.518725 + 00:00 app [web.1]: 2015-12-04T10:03:51.518728 + 00:00 app [web.1]: ActiveRecord :: RecordNotFound(无法找到'id'= 3的用户):
2015-12-04T10:03:51.518729 + 00:00 app [web.1]:
app / controllers / articles_controller.rb:31:在'show'2015-12-04T10:03:51.518730 + 00:00 app [web.1]: 2015-12-04T10:03:51.518730 + 00:00 app [web.1]: 2015-12-04T10:03:51.364367 + 00:00 app [web.1]:参数:{“id”=>“3”}
文章控制器中show方法的代码。
def show
@user = User.find(params[:id])
@article = Article.find(params[:id])
@comment = Comment.new
end
我的show.html.erb的代码
<h1 align="center">Title: <%= @article.title %></h1>
<div class="well col-xs-8 col-xs-offset-2">
<div class="row">
<div class="col-xs-4 col-xs-offset-4">
<div class="well well-sm">
<div class="user-img">
<p class="created">Created By:</p>
<%= gravatar_for @article.user, size: 150 %>
</div>
<div class="user-title">
<%= link_to @article.user.username, user_path(@article.user) %> <br />
</div>
</div>
</div>
</div>
<h4 class="center description"><strong>Description:</strong></h4>
<hr />
<%= simple_format(@article.description) %>
<div class="article-actions">
<% if logged_in? && current_user == @article.user %>
<%= link_to 'Edit', edit_article_path(@article), class: "btn btn- xs btn-primary" %>
<%= link_to 'Delete this article', article_path(@article), method: :delete,
data: { confirm: "Are you sure you want to delete this article?" },
class: "btn btn-xs btn-danger" %>
<%= link_to 'View all articles', articles_path, class: "btn btn-xs btn-success" %>
<% else %>
<%= link_to 'View all articles', articles_path, class: "btn btn-xs btn-success" %>
<% end %>
</div>
还有创建方法:
def create
redirect_to articles_path if !logged_in?
@articles = Article.all
@article = Article.new(article_params)
@article.user_id = current_user.id
if @article.save
flash[:success] = "Post created successfully!"
respond_to do |format|
format.html { redirect_to articles_path }
format.js
end
else
flash[:danger] = "We could not create you're article!"
render 'new'
end
端
答案 0 :(得分:1)
看起来你的控制器不正确:
def show
@user = User.find(params[:id]) <!==== Is this the problem?
@article = Article.find(params[:id])
@comment = Comment.new
end
答案 1 :(得分:1)
让我们做一个解决方案,因为你可以看到这个简单的错误导致了一个错误,让我花了一点时间才弄明白。 show方法中的错误; -
def show
@user = User.find(params[:id])
@article = Article.find(params[:id])
@comment = Comment.new
end
这里的错误是params [:id]用于在URL中查找ID,例如使用@user = User.find(params [:id]导致@user变量设置为其中的任何内容例如: articles / 5将user_id设置为5。
要解决此错误,请将show方法更改为以下内容:
def show
@article = Article.find(params[:id])
@user = User.find(@article.user_id)
@comment = Comment.new
end
这会将@user变量设置为查找文章user_id,而不是将其设置为文章ID。但是,在调用此方法之前,必须先定义@article,否则在尝试调用@article实例之前,这会失败。
这就是我设法解决这个问题的方法,希望它至少可以帮助其他人。
答案 2 :(得分:0)
是的,此代码可让您先找到用户,因为
@user = User.find(params [:id])
在您的代码中,您首先找到的用户:id = 3,这将不在数据库中,因此这将通过错误&#34; ActiveRecord :: RecordNotFound(无法找到用户) &#39; ID&#39; = 3):&#34; 强>
因此,要么删除代码的第一行,要么如果要查找&#34; article / 3&#34; ,则必须以&#34;用户/:USER_ID /文章/:ID&#34; 强>
使用嵌套资源
并将代码更改为
def show
@user = User.find(params[:user_id])
@article = Article.find(params[:id])
@comment = Comment.new
end