没有路线匹配[PATCH]" / posts / my-second-post"

时间:2018-03-02 04:54:29

标签: ruby-on-rails

我收到以下错误更新到博客应用(Rails)上的现有帖子:

我在收到错误时将author_posts_path更改为author_post_path

  

没有路线匹配[PATCH]" / posts / my-second-post"

现在,我收到如下新错误:

  

Blog :: Posts #index

中的ActionController :: UrlGenerationError      

显示C:/ Users / Royal和Carla / Desktop / testBlog3c / app / views / layouts / _navbar.html.erb,其中第18行引发:

     

没有路线匹配{:action =>" show",:controller =>" author / post"},缺少必需的键:[:id]

视图/布局/ _navbar.html.erb

16     </li> 
17        <li class="nav-item">
18        <%= link_to 'My posts', author_post_path, class: "nav-link #
          {yield(:author)}" %>
19     </li>   
20    </ul>
21  </div>

ontrollers /作者/ posts_controller.rb

private
      def set_post
        @post = Post.friendly.find(params[:id])
      end      

      def post_params
        params.require(:post).permit(:title, :body, :description, :banner_image_url)
      end

2 个答案:

答案 0 :(得分:0)

如果您的routes.rb文件看起来像这样,似乎是您没有传递帖子对象ID

namespace :author do 
  resources :posts
end

然后运行rake routes然后你会得到这样的输出

    author_posts GET    /author/posts(.:format)          author/posts#index
                 POST   /author/posts(.:format)          author/posts#create
 new_author_post GET    /author/posts/new(.:format)      author/posts#new
edit_author_post GET    /author/posts/:id/edit(.:format) author/posts#edit
     author_post GET    /author/posts/:id(.:format)      author/posts#show
                 PATCH  /author/posts/:id(.:format)      author/posts#update
                 PUT    /author/posts/:id(.:format)      author/posts#update
                 DELETE /author/posts/:id(.:format)      author/posts#destroy

见这个

edit_author_post GET    /author/posts/:id/edit(.:format) author/posts#edit
     author_post GET    /author/posts/:id(.:format)      author/posts#show
                 PATCH  /author/posts/:id(.:format)      author/posts#update
                 PUT    /author/posts/:id(.:format)      author/posts#update

:idedit&amp; {}需要update show如果需要编辑,则您的修改链接如下所示

edit_author_post_path(post_object_id) # object_id is a post.id

然后更新form_tag将如下所示

form_for [:author, @post] do |f|...

它应该有用,希望它会有所帮助。

答案 1 :(得分:0)

要更新帖子,首先需要编辑视图,并在此视图中请求更新操作。 所以我认为您首先将链接路径更改为edit_author_post(@ post)