简体   繁体   English

Ruby on Rails未定义方法

[英]Ruby on Rails Undefined method

I was going to create a link to a controller's action update_heimdall here is the code: 我打算创建到控制器动作的链接update_heimdall这里是代码:

app/controller/admins_controller : app/controller/admins_controller

  def heimdall
    @headline = "Heimdall"
    @users = User.where(deleted_at: nil)
    authorize @users
    @focusUser = params['users'].blank? ? current_user : User.find(params['users'])
    heim = Heimdall.new
    @cards = heim.get_cards(@focusUser.trello_id, @focusUser.trello_access_token, @focusUser.trello_secret_token)
  end

  def update_heimdall 
    @user = params['users'].blank? ? current_user : User.find(params['users'])
    authorize @user
    heim = Heimdall.new
    heim.update_cards(@user.trello_id, @user.trello_access_token, @user.trello_secret_token)
  end

app/views/admins/heimdall.html.rb : app/views/admins/heimdall.html.rb

<%= link_to "Odśwież", update_heimdall(), class: "btn btn-warning" %>

I am enclosing as well listing from rails routes command: 我也附上了来自rails route命令的清单:

heimdall GET    /heimdall(.:format) admins#heimdall                                        
update_heimdall GET   /heimdall/update_heimdall/:id(.:format)   admins#update_heimdall               

Unfortunately this code causes the following error: 不幸的是,此代码导致以下错误:

undefined method `update_heimdall'. 未定义的方法“ update_heimdall”。

As in your code, convert heim in to an instance variable @heim so that you can access it in views. 像在代码中一样,将heim转换为实例变量@heim以便可以在视图中访问它。 then call specifiy the object in action path. 然后在操作路径中指定对象。

When you performed rake routes you will get all the paths on left side 当您执行rake routes您将获得所有paths在左侧

heimdall GET    /heimdall(.:format) admins#heimdall                                        
update_heimdall GET   /heimdall/update_heimdall/:id(.:format)   admins#update_heimdall               

therefore your code should be: 因此您的代码应为:

<%= link_to "Odśwież", update_heimdall_path(@heim), class: "btn btn-warning" %>

for more information you can checkout this ruby on rails guide: http://guides.rubyonrails.org/getting_started.html 有关更多信息,您可以在rails指南上查看此ruby: http : //guides.rubyonrails.org/getting_started.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM