【问题标题】:UrlGenerationError Missing Required Key [:id] Error?UrlGenerationError 缺少必需的键 [:id] 错误?
【发布时间】:2016-08-26 09:56:54
【问题描述】:

当我想创建文章时,单击创建文章按钮后出现此错误

没有路由匹配 {:action=>"show", :controller=>"articles", :id=>nil} 缺少必需的键:[:id]

虽然我确保在创建文章之前有一个用户已登录。这是我的代码:

articles_controller.rb

class ArticlesController < ApplicationController
  before_action :require_user , except:[:index,:show]

  def index
      @articles = Article.all
  end  

  def new
    @article = Article.new
  end

  def create
    @article = Article.new(article_params)
    if @article.save
        flash[:notice] = "Your Article was Created"
    else
        render 'new'
    end
    redirect_to article_path(@article)
  end

  def show
    @article = Article.find(params[:id])
  end

  def edit
    @article = Article.find(params[:id])
  end

  def update
    @article = Article.find(params[:id])
    if @article.update(article_params)
        flash[:notice] = "Your Article was Updated"
        redirect_to article_path(@article)
    else
        render 'edit'
    end
  end

  def destroy
    @article = Article.find(params[:id])
    @article.destroy
    flash[:notice] = "Your Article was deleted"
    redirect_to articles_path(@article)
  end

  private

  def article_params
    params.require(:article).permit(:title,:description)
  end

结束

users_controller.rb

class UsersController < ApplicationController
  def index
    @users = User.all
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      flash[:success] = "You Signed up successfully"
      redirect_to articles_path
    else
      render 'new'
    end
  end

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

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

  def update
    @user = User.find(params[:id])
    if @user.update(user_params)
      flash[:success]= "Your user was updated"
      redirect_to articles_path
    else
      render 'edit'
    end
  end

  private

  def user_params
    params.require(:user).permit(:username,:email,:password)
  end

结束

views/new.html

    <h1>New Article</h1>
<%= form_for @article do |f| %>

<p>title<%= f.text_field :title %></p><br>
<p>description<%= f.text_area :description %></p>
<%= f.submit "Create" %>
<%end%>

这里是 my github repo

【问题讨论】:

  • 请检查您的创建文章按钮链接,它会将您重定向到显示文章控制器的操作
  • 在您的代码&lt;li&gt; &lt;%= link_to "New", new_article_path(@article)%&gt;&lt;/li&gt; 中。您不需要在new_article_path 中提供参数。否则,它会检查没有 id 的文章,正如 navin 所说。
  • 虽然我删除了参数,但它仍然显示错误。

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2


【解决方案1】:

当您创建无效文章时,它会给您missing required keys: [:id],因为它无法提供@article.save 它会redirect_to article_path(@article),在这种情况下@article.id 是nil

def create
    @article = Article.new(article_params)
    if @article.save
        flash[:notice] = "Your Article was Created"
    else
        render 'new'
    end
    redirect_to article_path(@article)
end

如果@article.save 为真,只需移动redirect_to article_path(@article)

def create
   @article = Article.new(article_params)
   if @article.save
       flash[:notice] = "Your Article was Created"
       redirect_to article_path(@article)
    else
       render 'new'
    end       
end

编辑:

我克隆了您的存储库,并且在创建没有 user_id 的文章时存在问题。在底部添加了更改,它似乎按预期工作。

def create
       @article = current_user.articles.build(article_params)

       if @article.save
           flash[:notice] = "Your Article was Created"
           redirect_to article_path(@article)
        else
           render 'new'
        end       
    end

希望有帮助!

【讨论】:

  • 问题是当代码移动到@article.save 时,即使有user_id 和标题和描述已验证,它也不会保存文章。
【解决方案2】:

你得到这个错误是因为 renderredirect 都在 @article.save 失败时执行。所以它转到 else 部分它被渲染到新的并再次重定向到显示页面 id = nil.

def create
   @article = Article.new(article_params)
   if @article.save
       flash[:notice] = "Your Article was Created"
       redirect_to article_path(@article.id) # or @article
    else
       render 'new'
    end       
end 

【讨论】:

  • 还是不行。当我单击创建文章按钮时,即使我将创建操作方法更改为此代码,它也不会保存文章。我尝试了控制台,它可以很好地创建文章,没有任何问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多