“Link_to在控制器中找不到操作,尽管操作已存在”

huangapple go评论55阅读模式
英文:

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 &#39;generate_book&#39; could not be found for PostsController):

&lt;%= link_to &quot;New Book&quot;, generate_book_post_path(post.id), data: { turbo_method: :post } %&gt;

# above code is within &lt;% @posts.each do |post| %&gt; 

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 输出。尝试以下步骤:

  1. 运行 rails routes 并检查是否存在此路由,请求方法和控制器操作是否正确指定。
  2. 检查服务器日志以查看请求方法和路径。它应该与步骤#1匹配。
  3. 确保在控制器中的操作结束后没有额外的 end
  4. 确保您的操作在控制器中未声明为 private
  5. 另外,对 app/ 之外的任何内容进行更改,如 config/routes.rb,都需要重新启动服务器。
英文:

Edit your question with full controller code and rails routes output. Try the following steps:

  1. Run rails routes and check if this route exists with correct request method and controller action specified.
  2. Check server logs for request method and path. It should match step #1
  3. Make sure to not have any extra end in your controller after your action ends.
  4. Make sure your action is not declared private in your controller.
  5. Also, making changes to anything outside of app/ such as config/routes.rb will require a server restart.

huangapple
  • 本文由 发表于 2023年5月25日 07:09:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327928.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定