Ruby on rails 使用按钮_将数据发送到控制器

Ruby on rails 使用按钮_将数据发送到控制器,ruby-on-rails,Ruby On Rails,这是按钮: <%= button_to "I want to understand this.", articles_want_to_understand_path, :id => @article.id, method: :post %> 在控制器中:- before_action :set_article, only: [:want_to_understand] def want_to_understand @article.user_who_wants_to_und

这是按钮:

<%= button_to "I want to understand this.", articles_want_to_understand_path, :id => @article.id, method: :post %>
在控制器中:-

before_action :set_article, only: [:want_to_understand]
def want_to_understand
  @article.user_who_wants_to_understand << current_user
  redirect_to action: :show
end

private

def set_article
  @article = Article.find(params[:id])
end
before\u action:set\u article,only:[:想要\u理解]
我想让你明白

@article.user_希望_理解这不是问题所在,问题是“for nil:NilClass”,这意味着控制器找不到文章。如果我将@article替换为article.first,它不会抛出该错误。请改用
articles\u want\u to\u understand\u path(id:@article.id)
。仍然会出现相同的错误。结果是,我必须将set\u article的内容粘贴到want\u to\u understand中。这样行吗?您还可以使用before_uu回调。
undefined method `user_who_wants_to_understand' for nil:NilClass
before_action :set_article, only: [:want_to_understand]
def want_to_understand
  @article.user_who_wants_to_understand << current_user
  redirect_to action: :show
end

private

def set_article
  @article = Article.find(params[:id])
end
<%= button_to "I want to understand this.", articles_want_to_understand_path(id: @article.id) %>