/ YYYY / MM / Структура URL-адреса Title-Slug с помощью Friendly_Id Решает проблему #new и #edit
У меня есть частичное решение моегопредыдущий выпуск который корректно отображает сообщения # index и posts # показывает маршруты, но задыхается после создания сообщения:
ActionController :: UrlGenerationError в PostsController # create
Не найдено ни одного маршрута {: action => "show",: controller => "posts"} отсутствуют необходимые ключи: [: id,: month,: year]
Извлеченный источник (вокруг строки № 32):
30 ответить_ сделать | форматировать |
31 если @ post.save
32 format.html {redirect_to post_path, обратите внимание: «Сообщение успешно создано». }
33 format.json {рендер: шоу, статус:: создан, местоположение: @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
routes.rbRails.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
post.rbclass 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
posts_controller.rbclass 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
posts_helper.rbmodule PostsHelper
def post_path(post)
"blog/#{post.year}/#{post.month}/#{post.slug}"
end
end
приложение / просмотров / сообщений / index.html.erb<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 %>
В итоге вот что работает:
/ Блог / индекс/ Блог / 2015/09 / пример-сообщениеСоздание нового сообщения (вплоть до того момента, когда оно должно перенаправляться на сообщения # show, когда вы получите вышеупомянутую UrlGenerationError)Тем не менее, новое сообщение добавляется в БД, поэтому, если вы вернетесь к / index, новое сообщение будет видноУничтожение поста... Что не работает:
Редактирование поста (будет отредактирована страница редактирования с формой, но после отправки изменений вы получите вышеупомянутую ошибку - и изменения никогда не внесут ее в БД)Завершение редиректа после создания нового поста (упоминалось ранее)./ Блог / 2015 / индекс/ Блог / 2015/09 / индексМеня радует, что я зашел так далеко - любые рекомендации по решению этих нерешенных вопросов будут очень благодарны!
РЕДАКТИРОВАТЬ
Благодаря @ 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
И теперь ссылка «изменить» со страницы индекса идет на правильный URL (/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 доpatch '/:year/:month/:id', to: 'posts#update'
Назовите обновленный маршрут PATCHas: 'patch'
и добавьте переопределение пути PATCH к posts_helper:
def patch_path(post)
"#{post.year}/#{post.month}/#{post.slug}"
end
Измените переопределение на:
def patch_path(post)
""
end
Отмените имя и измените маршрут PATCH на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, где оно заканчивается удушьем. Идеи?