【问题标题】:Heroku getting wrong IDHeroku 得到错误的 ID
【发布时间】: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:in show'`这是你可以调试的

标签: ruby-on-rails ruby


【解决方案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 否则会失败,因为您在定义 this 之前尝试调用 @article 实例。

这就是我设法弄清楚的方法,希望它至少可以帮助其他人。

【讨论】:

    【解决方案2】:

    您的控制器似乎不正确:

    def show
      @user = User.find(params[:id]) <!==== Is this the problem?
      @article = Article.find(params[:id])
      @comment = Comment.new
    end
    

    【讨论】:

    • 是的,我刚刚发布了另一个答案 - 解释问题,以防其他人遇到同样的问题。这么小的东西可能会导致这样的问题。
    【解决方案3】:

    是的,这段代码会让你首先找到用户,因为

    @user = User.find(params[:id])

    在您的代码中,您首先找到 :id = 3 的用户,该用户不在数据库中,因此这将通过错误 "ActiveRecord::RecordNotFound (Couldn't find User with 'id'=3 ):"

    所以,要么删除你的代码的第一行,要么如果你想找到 "article/3" 你必须创建不同的路由 "users/:user_id/articles/:身份证”

    使用嵌套资源

    并将代码更改为

    def show
      @user = User.find(params[:user_id])
      @article = Article.find(params[:id])
      @comment = Comment.new
    end
    

    【讨论】:

      猜你喜欢
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-27
      • 2018-07-20
      • 1970-01-01
      • 2020-02-05
      相关资源
      最近更新 更多