Rails 出现路由错误:没有路由匹配 [PATCH] "/topics/4/posts"



当我试图用摘要更新帖子时,遇到了一个问题。在模型部分,每个帖子都有一个摘要。

当我尝试更新或创建一个具有相同形式摘要的帖子时,会出现一个路由错误,即

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"

如果您查看该表在这里你会看到你需要一个你发布的id。您应该修复模板中的链接:/topics/4/posts

它必须是这样的:/topics/4/posts/6

正如@Grosefaid所说,您使用:id而不是:post_id

如果您真的必须指定URL,那么您可以使用topic_item_path(topic, post)而不是自己构建它。

更好的是,由于您已经向form_for提供了[topic, post],您只需删除url选项,表单就可以正确生成。

最新更新