我对我的previous issue有部分解决方案正确显示帖子#index和posts#show routes,但在创建帖子后窒息:
PostsController中的ActionController :: UrlGenerationError #create
没有路由匹配{:action =>“show”,:controller =>“posts”}缺少必需的密钥:[:id,:month,:year]提取的来源(第32行):
30 respond_to do | format |
31 if @ post.save
32 format.html {redirect_to post_path,注意:'帖子已成功创建。' }
33 format.json {render:show,status :: created,location:@post}
34别的
35 format.html {render:new}
...并编辑帖子:
没有路线匹配[PATCH]“/ blog / example-post / blog / 2015/09 / example-post”
以下是所有相关文件(使用相同的非常简单的脚手架博客):
$ rails new blog
[...]
$ cd blog
# (Add friendly_id to Gemfile & install)
$ rails generate friendly_id
$ rails generate scaffold post title content slug:string:uniq
[...]
$ rake db:migrate
Rails.application.routes.draw do
scope 'blog' do
get '', to: 'posts#index', as: 'posts'
post '', to: 'posts#create'
get '/new', to: 'posts#new', as: 'new_post'
get '/:id/edit', to: 'posts#edit', as: 'edit_post'
get '/:year/:month/:id', to: 'posts#show', as: 'post'
patch '/:id', to: 'posts#update'
put '/:id', to: 'posts#update'
delete '/:year/:month/:id', to: 'posts#destroy'
end
end
class Post < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
def year
created_at.localtime.strftime("%Y")
end
def month
created_at.localtime.strftime("%m")
end
end
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
def index
@posts = Post.all
end
def show
@post = Post.friendly.find(params[:id])
end
def new
@post = Post.new
end
def edit
@post = Post.friendly.find(params[:id])
end
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to post_path, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to post_path, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :content, :slug)
end
end
end
module PostsHelper
def post_path(post)
"blog/#{post.year}/#{post.month}/#{post.slug}"
end
end
<p id="notice"><%= notice %></p>
<h1>Listing Posts</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Content</th>
<th>Slug</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.content %></td>
<td><%= post.slug %></td>
<td><%= link_to 'Show', post_path(post) %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Post', new_post_path %>
总之,这是有效的:
......什么行不通:
我很激动我已经走到了这一步 - 任何解决这些悬而未决问题的指导都会非常感激!
修改
感谢@ brad-werth,通过以下更改修复了帖子创建:
posts_controller.rb
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to post_path(@post.year, @post.month, @post), notice: 'Post was successfully created.' }
我也尝试过以下方式解决帖子编辑问题:
将编辑路线更改为get '/:year/:month/:id/edit', to: 'posts#edit', as: 'edit_post'
并将以下覆盖添加到posts_helper.rb以防止索引页面中断:
def edit_post_path(post)
"#{post.year}/#{post.month}/#{post.slug}/edit"
end
现在,索引页面中的“编辑”链接将转到正确的网址(/blog/2015/09/example-post/edit
- 它曾用于/blog/example-post/edit
)并成功呈现编辑页面。但这会导致PATCH中断(实际上,更新不会进入数据库):
No route matches [PATCH] "/blog/2015/09/example-post/blog/2015/09/example-post"
我认识到这个重复问题可能在于此edit_post_path覆盖,但以下尝试强制使用正确的PATCH路由无效:
patch '/:year/:month/:id', to: 'posts#update'
将更新后的PATCH路由命名为as: 'patch'
,并将PATCH路径覆盖添加到posts_helper:
def patch_path(post)
"#{post.year}/#{post.month}/#{post.slug}"
end
将覆盖更改为:
def patch_path(post)
""
end
patch '', to: 'posts#update'
看看posts_controller,它看起来不像问题,因为它不是重定向不是问题 - 我不明白为什么@post.update(post_params)
会有问题:
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
因此,我可以告诉URL中的重复发生在PATCH操作之前,这会将我们带回EDIT流程 - 它必须将复制传递给PATCH,它会导致阻塞。想法?
答案 0 :(得分:0)
您的错误说明:
No route matches {:action=>"show", :controller=>"posts"} missing required keys: [:id, :month, :year]
正如您在失败的行format.html { redirect_to post_path, notice: 'Post was successfully created.' }
中看到的那样,您在没有参数的情况下调用post_path
。
您的路线get '/:year/:month/:id', to: 'posts#show', as: 'post'
需要年,月和身份证明。这与您上面的错误消息一致。
要修复,只需提供缺少的参数,如下所示:
format.html { redirect_to post_path(@post.year, @post.month, @post), notice: 'Post was successfully created.' }
答案 1 :(得分:0)
更新和创建操作更改:
format.html { redirect_to post_path, notice: 'Post was successfully ...' }
与
format.html { redirect_to @post, notice: 'Post was successfully ...' }