【发布时间】:2015-12-04 18:55:12
【问题描述】:
所以我正在使用 heroku 来演示我的 ruby on rails 应用程序,并制作我的个人网站。然而,对于一个 live heroku 应用程序,url 文章/2 不是获取帖子的 ID,而是试图获取 user_id。假设您有 5 篇文章,但有 3 个用户。然后你尝试去文章/5,它会给你一个错误,在日志中。
5-12-04T10:03:51.370101+00:00 app[web.1]: Completed 404 Not Found in 6ms (ActiveRecord: 4.3ms)
2015-12-04T10:03:51.361893+00:00 app[web.1]:开始获取 2015-12-04 10:03:51 +0000 时为 46.237.130.242 的“/articles/3”
2015-12-04T10:03:51.513724+00:00 app[web.1]:参数:{"id"=>"3"} 2015-12-04T10:03:51.513671+00:00 app[web.1]: 处理者 文章控制器#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 应用[web.1]: 2015-12-04T10:03:51.518728+00:00 应用 [web.1]: ActiveRecord::RecordNotFound(找不到 'id'=3 的用户):
2015-12-04T10:03:51.518729+00:00 应用[web.1]:
app/controllers/articles_controller.rb:31:in `show'2015-12-04T10:03:51.518730+00:00 应用[web.1]: 2015-12-04T10:03:51.518730+00:00 应用[web.1]: 2015-12-04T10:03:51.364367+00:00 app[web.1]:参数:{"id"=>"3"}
文章控制器中显示方法的代码。
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>
还有create方法:
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
结束
【问题讨论】:
-
在您的 heroku 日志中,显然状态为
2015-12-04T10:03:51.518725+00:00 app[web.1]: 2015-12-04T10:03:51.518728+00:00 app[web.1]: ActiveRecord::RecordNotFound (Couldn't find User with 'id'=3): -
app/controllers/articles_controller.rb:31:inshow'`这是你可以调试的
标签: ruby-on-rails ruby