英文:
Link_to not finding action in controller even though the action is there
问题
我有以下的视图。基本上,一旦你点击它,它应该生成一个新的图书帖子。每个图书帖子都与一个帖子相关联。但是我一直得到这个错误:AbstractController::ActionNotFound(找不到PostsController的动作'generate_book'):
<%= link_to "New Book", generate_book_post_path(post.id), data: { turbo_method: :post } %>
# 上述代码位于<% @posts.each do |post| %>之内
这是我的帖子控制器:
def generate_book
post = Post.find(params[:id])
book_output = GetBook.call # 调用服务
Book.create(post: post, one: book_output)
redirect_to books_path
end
我的路由:
resources :posts do
post :generate_book, on: :member
end
我确保没有拼写错误。我真的很感激帮助。
英文:
I have the following View. Basically once you click it, it should generate a new book post. Each book post is associated with a post. But I keep getting this error: AbstractController::ActionNotFound (The action 'generate_book' could not be found for PostsController):
<%= link_to "New Book", generate_book_post_path(post.id), data: { turbo_method: :post } %>
# above code is within <% @posts.each do |post| %>
Here's my posts controller:
def generate_book
post = Post.find(params[:id])
book_output = GetBook.call # calls services
Book.create(post: post, one: book_output)
redirect_to books_path
end
My routes:
resources :posts do
post :generate_book, on: :member
end
I've made sure there's no typo. I'd really appreciate help.
答案1
得分: 1
编辑您的问题,附上完整的控制器代码和 rails routes
输出。尝试以下步骤:
- 运行
rails routes
并检查是否存在此路由,请求方法和控制器操作是否正确指定。 - 检查服务器日志以查看请求方法和路径。它应该与步骤#1匹配。
- 确保在控制器中的操作结束后没有额外的
end
。 - 确保您的操作在控制器中未声明为
private
。 - 另外,对
app/
之外的任何内容进行更改,如config/routes.rb
,都需要重新启动服务器。
英文:
Edit your question with full controller code and rails routes
output. Try the following steps:
- Run
rails routes
and check if this route exists with correct request method and controller action specified. - Check server logs for request method and path. It should match step #1
- Make sure to not have any extra
end
in your controller after your action ends. - Make sure your action is not declared
private
in your controller. - Also, making changes to anything outside of
app/
such asconfig/routes.rb
will require a server restart.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论