ruby-on-rails - UrlGenerationError 缺少必需的 key [:id] Error?

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

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

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

希望对您有所帮助!

关于ruby-on-rails - UrlGenerationError 缺少必需的 key [:id] Error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39163536/

相关文章:

ruby-on-rails - Rails——如何将 HAML 部分的 HTML 输出保存为字符串以发送到外部服务?

ruby-on-rails - 如何在 Rails/Ruby 中获取包含哈希值的完整 URL

ruby - 无法将提交推送到 heroku

ruby-on-rails - 在具有两个日期属性的 Rails 中进行验证

ruby-on-rails - 太阳黑子:更新子模型时父模型的力指数

ruby-on-rails - 带有Youtube API请求的Fancybox

ruby-on-rails - 功能测试无法识别任何路线

ruby-on-rails - 当密码也存在或已更改时,Rails 会验证 password_confirmation 是否存在

ruby-on-rails - 没有路由匹配 { :action= >“show” , :controller= >“users” }

ruby - Amazon Selling Partner API - 签名请求(ruby 实现)