- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
根据我在 earlier question 中获得的指导在解决我的 original issue to implement /YYYY/MM/Slug URL structure ,我希望得到一些帮助来解决我在尝试编辑帖子时收到的错误:
No route matches [PATCH] "/blog/2015/09/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 '/:year/:month/: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(@post.year, @post.month, @post), 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
def edit_post_path(post)
"#{post.year}/#{post.month}/#{post.slug}/edit"
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 %>
总而言之,这是有效的:
…什么不起作用:
我认识到这个重复问题可能与此 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,它最终会阻塞。想法?提前致谢!
编辑
编辑.html.erb
<h1>Editing Post</h1>
<%= render 'form' %>
<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>
_form.html.erb
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br>
<%= f.text_field :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
最佳答案
看起来主要问题出在帮助程序中。返回的路径需要以斜杠开头:
module PostsHelper
def post_path(post)
"/blog/#{post.year}/#{post.month}/#{post.slug}"
end
def edit_post_path(post)
"/blog/#{post.year}/#{post.month}/#{post.slug}/edit"
end
end
如果没有开头的斜杠,浏览器会将它们视为相对路径,将它们附加到当前路径(这就是为什么你以 /blog/2015/09/example-post/blog/2015/09/结尾的原因example-post
提交编辑表单时)。
您还需要确保您的 patch
和 put
路由一致:
get '/:year/:month/:id', to: 'posts#show', as: 'post'
patch '/:year/:month/:id', to: 'posts#update'
put '/:year/:month/:id', to: 'posts#update'
delete '/:year/:month/:id', to: 'posts#destroy'
最后, Controller 需要包含 PostsHelper
并将其方法用于 create
和 update
中的重定向 URL:
class PostsController < ApplicationController
include PostsHelper
...
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to post_path(@post), 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(@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
...
end
关于ruby-on-rails -/YYYY/MM/Title-Slug URL 结构与 Friendly_Id Solution Chokes on #edit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32658204/
如果我在命令行上运行以下 PHP 代码,我会得到预期的结果: php -r 'var_dump(ldap_connect("ldaps://ldaps.example.com", 636));' re
我在 Google 上找到了很多关于此问题的搜索结果,但找不到解决方案。 当我尝试使用 ikvmstub.exe 将 .NET-Interface 转换为 Java stub 时,出现以下错误: C:
尝试使用 C# 和 System.XML.Serialization 创建 Excel 2007 XML 文件。 Excel XML 文件的根元素是这样的: 默认命名空间中的许多元素都具有用“ss”
根据我在 earlier question 中获得的指导在解决我的 original issue to implement /YYYY/MM/Slug URL structure ,我希望得到一些帮助
在新的 Ubuntu 14.04 机器上重现错误的步骤。 从以前安装的 freebsd 机器的/usr 获取 include 和 lib 文件夹。 在ubuntu机器的主目录中设置文件夹。 现在,要运
我是一名优秀的程序员,十分优秀!