UrlGenerationError 缺少必需的密钥 [:id] 错误?

UrlGenerationError Missing Required Key [:id] Error?

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

No route matches {:action=>"show", :controller=>"articles", :id=>nil} missing required keys: [: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

当您创建无效文章时,它会给您 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

希望对您有所帮助!

你得到这个错误是因为 renderredirect 都在 @article.save[ 时执行=17=] fails.So 它转到 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