"articles", :id=>nil} missi-6ren">
gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:08:29 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com