当我想创建文章时,我在点击创建文章按钮
后收到此错误没有路由匹配{: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
端
视图/ 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
答案 0 :(得分:2)
当您创建无效文章时,它将为您missing required keys: [:id]
提供@article.save
,因为它redirect_to article_path(@article)
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.id为redirect_to article_path(@article)
@article.save
如果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
修改强>
我克隆了您的仓库,在创建没有[_id] => MongoId Object (
[$id] => 57beda7f0640fd14ca5cc307
)
[website] => MongoId Object (
[$id] => 57beda3d279871c80e8b4567
)
[url] => https://acb.com
[meta_tags] => Array (
[description] =>
[title] => Login
[title_length] => 18
)
的文章时存在问题。在底部添加了更改,它似乎按预期工作。
$this->website->where('website', $website)->get();
希望有所帮助!
答案 1 :(得分:1)
由于渲染和重定向,当 @ article.save 失败时,您收到此错误。因此,它会转到其他部分它会渲染到新的并再次重定向到显示 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