没有路由匹配{:controller =>“generators”,:action =>“new”}缺少必需的键:[:user_id]

时间:2013-12-05 12:46:41

标签: ruby-on-rails ruby-on-rails-4

我正在尝试使用此代码,但我不知道应该如何修复它。我得到的错误是

No route matches {:controller=>"generators", :action=>"new"} missing required keys: [:user_id]

用户

 has_many :generators
      has_many :results, :through=>:generators

发电机

has_one :result , :dependent => :destroy
  belongs_to :user

RESULT

belongs_to :generator
   belongs_to :user, :through=>:generators

这是我的主页:

 <%= link_to 'HOME', root_path, id:'home' %>
                     <%= link_to 'GENERATE RAPD PRIMER',new_user_generator_path, id:'new' %></li>
                     <%= link_to 'VIEW RESULT HISTORY', user_generators_path,id:'view'%></li>

我的佣金路线是:

      Prefix Verb   URI Pattern                                   Controller#Action
               signout GET    /signout(.:format)                            sessions#destroy
                  root GET    /                                             welcome#index
                       GET    /auth/:provider/callback(.:format)            sessions#create
auth_identity_callback POST   /auth/identity/callback(.:format)             sessions#create
       user_generators GET    /users/:user_id/generators(.:format)          generators#index
                       POST   /users/:user_id/generators(.:format)          generators#create
    new_user_generator GET    /users/:user_id/generators/new(.:format)      generators#new
   edit_user_generator GET    /users/:user_id/generators/:id/edit(.:format) generators#edit
        user_generator GET    /users/:user_id/generators/:id(.:format)      generators#show
                       PATCH  /users/:user_id/generators/:id(.:format)      generators#update
                       PUT    /users/:user_id/generators/:id(.:format)      generators#update
                       DELETE /users/:user_id/generators/:id(.:format)      generators#destroy
          user_results GET    /users/:user_id/results(.:format)             results#index
                       POST   /users/:user_id/results(.:format)             results#create
       new_user_result GET    /users/:user_id/results/new(.:format)         results#new
      edit_user_result GET    /users/:user_id/results/:id/edit(.:format)    results#edit
           user_result GET    /users/:user_id/results/:id(.:format)         results#show
                       PATCH  /users/:user_id/results/:id(.:format)         results#update
                       PUT    /users/:user_id/results/:id(.:format)         results#update
                       DELETE /users/:user_id/results/:id(.:format)         results#destroy
                 users GET    /users(.:format)                              users#index
                       POST   /users(.:format)                              users#create
              new_user GET    /users/new(.:format)                          users#new
             edit_user GET    /users/:id/edit(.:format)                     users#edit
                  user GET    /users/:id(.:format)                          users#show
                       PATCH  /users/:id(.:format)                          users#update
                       PUT    /users/:id(.:format)                          users#update
                       DELETE /users/:id(.:format)                          users#destroy
            identities GET    /identities(.:format)                         identities#index
                       POST   /identities(.:format)                         iden
          new_identity GET    /identities/new(.:format)                     iden
         edit_identity GET    /identities/:id/edit(.:format)                iden
              identity GET    /identities/:id(.:format)                     iden
                       PATCH  /identities/:id(.:format)                     iden
                       PUT    /identities/:id(.:format)                     iden
                       DELETE /identities/:id(.:format)                     iden

每当some1点击GENERATE RAPD PRIMER时,他/她必须首先登录才可以使用它。所以在这个错误中我该如何修复它们?

2 个答案:

答案 0 :(得分:1)

在您的发电机控制器中,将@user设置为当前用户

@user = current_user 
#handle this however you want - define the user that is on the page

然后在视图中:

<%= link_to 'GENERATE RAPD PRIMER', new_user_generator_path(id:@user.id) %>

答案 1 :(得分:1)

<强> generators_controller

@user = current_user
视图中的

<%= link_to 'GENERATE RAPD PRIMER',new_user_generator_path(@user), id:'new' %></li>
<%= link_to 'VIEW RESULT HISTORY', user_generators_path(@user),id:'view'%></li>