【问题标题】:No route matches [PATCH] "/posts/my-second-post"没有路线匹配 [PATCH] "/posts/my-second-post"
【发布时间】:2018-03-02 04:54:29
【问题描述】:

我收到以下错误,更新博客应用程序 (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>

onrollers/author/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

【问题讨论】:

  • 可以显示你的routes.rb文件

标签: ruby-on-rails


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    如果您的 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
    

    您需要:id 获取editupdateshow 如果需要编辑,那么您的编辑链接如下所示

    edit_author_post_path(post_object_id) # object_id is a post.id
    

    然后更新form_tag 会是这个样子

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

    它应该可以工作,希望它会有所帮助。

    【讨论】:

    • 谢谢。这是我的路由文件: Rails.application.routes.draw 做根到:'blog/posts#index' 命名空间:作者做资源:posts 结束范围模块:'blog' 做得到'about' => 'pages#about' , as: :about get 'contact' => 'pages#contact', as: :contact get 'posts' => 'post#index', as: :posts get 'posts/:id' => 'posts#show ', as: :post end # 有关此文件中可用的 DSL 的详细信息,请参阅guides.rubyonrails.org/routing.html end
    • 我检查了我的文件,没有看到你提到的表单标签。
    • 请描述您的问题如何更新现有帖子,我正在为您写一个答案
    • 不确定我是否理解?
    • 我在我的 _form.html.erb 中找到了这个:

      禁止保存此帖子:

    猜你喜欢
    • 2018-07-11
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多