我收到以下错误更新到博客应用程序(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
要更新帖子,首先需要编辑视图,并在此视图中请求更新操作。所以我认为你第一步改变链接路径到edit_author_post(@post)
如果你的routes.rb
文件看起来像这样,你似乎没有传递post对象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
为edit
,update
和show
如果需要编辑然后你的编辑链接看起来像这样
edit_author_post_path(post_object_id) # object_id is a post.id
然后更新form_tag
将会是这样的
form_for [:author, @post] do |f|...
它应该工作,希望它会有所帮助。