当我尝试使用摘要更新帖子时,我遇到了一个问题。在模型部分中,每个帖子都有一个摘要。
当我尝试使用相同的表单更新或创建包含摘要的帖子时,会出现路由错误,同一个:
No route matches [PATCH] "/topics/4/posts"
Rails.application.routes.draw do
resources :advertisements, only: [:index, :show]
devise_for :users
resources :users, only: [:update, :show, :index]
resources :topics do
resources :posts, except: [:index], controller: 'topics/posts' do
end
end
resources :posts, only: [:index] do
resources :summaries, only: [:create, :update]
resources :comments, only: [:create, :destroy]
resources :favorites, only: [:create, :destroy]
post '/up-vote' => 'votes#up_vote', as: :up_vote
post '/down-vote' => 'votes#down_vote', as: :down_vote
end
这些路线看起来是否正确?如果是这样,它可能是控制器方法。帖子和摘要是单独的模型,所以考虑到这一点对我来说有点棘手。
def create
@topic = Topic.find(params[:topic_id])
@post = current_user.posts.build(post_params)
@post.topic = @topic
authorize @post
if @post.save
@summary = @post.build_summary(summary_params)
if @summary.save
@post.create_vote
flash[:notice] = "Post was saved."
redirect_to [@topic, @post]
else
flash[:error] = "There was an error saving the post. Please try again."
render :new
end
end
end
def update
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
@summary = @post.summary
@post.topic = @topic
authorize @post
if @post.update_attributes(post_params) && @summary.update_attributes(summary_params)
flash[:notice] = "Post was updated."
redirect_to [@topic, @post]
else
flash[:error] = "There was an error saving the post. Please try again."
render :edit
end
end
class SummariesController < ApplicationController
def new
@post = Post.find(params[:post_id])
@summary = @post.build(summary_params)
authorize @summary
def update
@post = Post.find(params[:post_id])
@summary = @summary.update_attributes(summary_params)
end
private
def summary_params
params.require(:summary).permit(:body)
end
end
我的路线看起来很乱,因为目前他们不允许我在验证后更新帖子或渲染任何错误。
这是帖子表格。摘要有一个验证,但这里没有检查。这是一个嵌套的形式,所以我有点卡在这里如何解释。
<%= form_for [topic, post], :url => "/topics/#{params[:topic_id]}/posts/#{params[:post_id]}", :html => {:multipart => true} do |f| %>
<% if post.errors.any? %>
<div class="alert alert-danger"
<h4>There are <%= pluralize(post.errors.count, "error") %>.</h4>
<ul>
<% post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_group_tag(post.errors[:title]) do %>
<%= f.label :title %>
<%= f.text_field :title, class: 'form_control', placeholder: "Enter post title" %>
<% end %>
<%= form_group_tag(post.errors[:body]) do %>
<%= f.label :body %>
<%= f.text_area :body, rows: 8, class: 'form-control', placeholder: "Enter post body" %>
<% end %>
<p>Add a picture</p>
<% if post.image? %>
<div class="form-group">
<%= image_tag( post.image_url) %>
</div>
<% end %>
<div class="form-group">
<%= f.label :image %>
<%= f.file_field :image %>
<%= f.hidden_field :image_cache %>
</div>
<div class="form-group">
<%= f.fields_for :summary, post.summary do |s| %>
<%= s.hidden_field :post_id, :value => post.id %>
<%= s.label :body, "Summary" %>
<%= s.text_field :body, class: 'form-control', placeholder: "Enter a 1-2 sentence summary" %>
<% end %>
</div>
<div class="form-group">
<%= f.submit "Save", class: 'btn btn-success' %>
</div>
<% end %>
将标题从“PUT”更改为“PATCH”
答案 0 :(得分:1)
如果你看看桌子
here你会发现你需要发布你的身份证明。您应修复模板中的链接:/topics/4/posts
必须如下:/topics/4/posts/6
答案 1 :(得分:0)
如@Grosefaid所述,您使用:id
代替:post_id
。
如果您确实需要指定网址,那么您可以使用topic_item_path(topic, post)
而不是自己构建网址。
更好的是,由于您已向[topic, post]
提供了form_for
,因此您只需删除url
选项即可正确生成。