0

我正在爲現有數據庫製作網站。這個數據庫來自遊戲,我無法對現有表格做很多更改。Rails 5 with Devise - 設計用戶has_one登錄

我決定使用Devise作爲認證解決方案。 我將使用Devise的用戶模型作爲網站,數據庫有一個包含一些重要信息的登錄表。

他們將有一個一對一協會其中用戶has_one登錄和登錄屬於用戶。

我更改了用戶註冊表以獲得一些登錄信息。我爲此使用了嵌套形式。當我註冊時,它應該創建用戶和登錄。

我想這個創建部分,但它不工作。我嘗試了很多不同的方式,但它從來不行這兩個主要問題是:

  1. 它創建的用戶,但不創建登錄
  2. 它同時創建,但登錄沒有,我把形式 的信息。他們都是空白

我嘗試了許多不同的方法:

  • 覆蓋在這個RegistrationController新方法使用resource.build_login內
  • 覆蓋sign_up_params與許可方法把resource.build_login在新視圖
  • 覆蓋build_resource並把resource.build_login有

沒有一個可行。

我正在使用Rails 5.0.2和Devise 4.2.0。

這些機型:

class Login < ApplicationRecord 
    belongs_to :user 
    # accepts_nested_attributes_for :user 
end 

class User < ApplicationRecord 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    has_one :login 
    # accepts_nested_attributes_for :login 
end 

一個重要的問題是我應該把accepts_nested_attributes_for?我看到有人在模型中使用它,並且我還看到人們在has_one模型中使用它。

這是我這個RegistrationController:

class Users::RegistrationsController < Devise::RegistrationsController 
    def new 
    super 
    resource.build_login 
    end 
    def create 
    super 
    end 
    def sign_up_params 
    params.require(resource_name).permit(:email, :password, 
        :password_confirmation, login_attributes: 
        [:userid, :birthdate, :sex]) 
    end 
end 

這是我的看法:

<%= simple_form_for(resource, as: resource_name, url: 
        registration_path(resource_name)) do |f| %> 
    <%= f.error_notification %> 
    <%= f.input :email, required: true, autofocus: true, class: 'form- 
       control' %> 
    <%= f.input :password, required: true, hint: ("No mínimo # 
       {@minimum_password_length} caracteres" if 
       @minimum_password_length) %> 
    <%= f.input :password_confirmation, required: true %> 
    <%= f.simple_fields_for :login do |ff| %> 
    <%= ff.input :userid %> 
    <%= ff.input :birthdate %> 
    <%= ff.label :sex %> 
    <%= ff.input_field :sex, collection: [['Feminino', 'F'], 
       ['Masculino', 'M']], class: 'form-control' %> 
    <% end %> 
    <%= f.button :submit, 'Cadastrar', class: 'btn btn-primary' %> 
<% end %> 

我希望所有需要的信息都在那裏。

我發現這個問題大多數的回答是到Rails 4或者Rails 3,也許他們並沒有因爲我使用Rails 5.爲我工作

編輯#1:添加在這個RegistrationController創建方法。我們將accepts_nested_attributes_for更改爲登錄模式。

這裏是日誌的代碼怎麼現在是:

Started POST "/users" for 127.0.0.1 at 2017-07-14 20:24:40 -0300 
Processing by Users::RegistrationsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=> 
     "habj5iep5SaE5nkyimzFyVslsCAIvy7ZokmM 
     KA8WDgbgQkYCa/mofo83sllRiwX13OCkPXIjbkR+CcDKBICXOw==", 
     "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", 
     "password_confirmation"=>"[FILTERED]", "login"=> 
     {"userid"=>"breno1", "birthdate"=>"13/07/2017", "sex"=>"M"}}, 
     "commit"=>"Cadastrar"} 
Unpermitted parameter: login 
(10.8ms) BEGIN 
User Exists (10.1ms) SELECT 1 AS one FROM `users` WHERE 
    `users`.`email` = BINARY '[email protected]' LIMIT 1 
SQL (11.5ms) INSERT INTO `users` (`email`, `encrypted_password`, 
    `created_at`, `updated_at`) VALUES ('[email protected]', 
    '$2a$11$4le6ZB5JyrYZGYLXvtlwa.c6BA463BvNYkSINfU0C10LoJADEYJ8q', 
    '2017-07-14 23:24:41', '2017-07-14 23:24:41') 
(18.3ms) COMMIT 
(12.6ms) BEGIN 
SQL (11.0ms) UPDATE `users` SET `sign_in_count` = 1, 
    `current_sign_in_at` = '2017-07-14 23:24:41', `last_sign_in_at` = 
    '2017-07-14 23:24:41', `current_sign_in_ip` = '127.0.0.1', 
    `last_sign_in_ip` = '127.0.0.1' WHERE `users`.`id` = 33 
(11.6ms) COMMIT 
Redirected to http://localhost:3000/ 
Completed 302 Found in 436ms (ActiveRecord: 85.8ms) 


Started GET "/" for 127.0.0.1 at 2017-07-14 20:24:41 -0300 
Processing by HomeController#index as HTML 
Rendering home/index.html.erb within layouts/application 
Rendered home/index.html.erb within layouts/application (0.7ms) 
User Load (13.7ms) SELECT `users`.* FROM `users` WHERE `users`.`id` 
    = 33 ORDER BY `users`.`id` ASC LIMIT 1 
Login Load (11.1ms) SELECT `login`.* FROM `login` WHERE 
    `login`.`user_id` = 33 LIMIT 1 
Rendered layouts/_side_navbar.html.erb (26.0ms) 
Completed 500 Internal Server Error in 619ms (ActiveRecord: 24.8ms) 



ActionView::Template::Error (undefined method `userid' for 
    nil:NilClass): 
4:  <div class="sidenav-header-inner text-center"> 
5:   <%= image_tag 'avatar-1.jpg', class: 'img-fluid rounded- 
    circle' %> 
6:   <h2 class="h5 text-uppercase"> 
7:   <%= current_user.login.userid %> 
8:   </h2> 
9:   <span class="text-uppercase"> 
10:   <%= current_user.is_admin? ? 'administrador' : 'jogador' 
    %> 

app/views/layouts/_side_navbar.html.erb:7:in 
`_app_views_layouts__side_navbar_html_erb__415501206315934300_ 
    70011534596860' 
app/views/layouts/application.html.erb:13:in 
`_app_views_layouts_application_html_erb__1458307398416558594_ 
    70011827322320' 
Rendering /home/prikster/.rvm/gems/ruby-2.3.1/gems/actionpack- 
    5.0.2/lib/action_dispatch/middleware/templates/rescues/ 
    template_error.html.erb within rescues/layout 
Rendering /home/prikster/.rvm/gems/ruby-2.3.1/gems/actionpack- 
    5.0.2/lib/action_dispatch/middleware/templates/rescues/ 
    _source.html.erb 
Rendered /home/prikster/.rvm/gems/ruby-2.3.1/gems/actionpack- 
    5.0.2/lib/action_dispatch/middleware/templates/rescues/ 
    _source.html.erb (32.9ms) 
Rendering /home/prikster/.rvm/gems/ruby-2.3.1/gems/actionpack- 
    5.0.2/lib/action_dispatch/middleware/templates/rescues/ 
    _trace.html.erb 
Rendered /home/prikster/.rvm/gems/ruby-2.3.1/gems/actionpack- 
    5.0.2/lib/action_dispatch/middleware/templates/rescues/ 
    _trace.html.erb (5.0ms) 
Rendering /home/prikster/.rvm/gems/ruby-2.3.1/gems/actionpack- 
    5.0.2/lib/action_dispatch/middleware/templates/rescues/ 
    _request_and_response.html.erb 
Rendered /home/prikster/.rvm/gems/ruby-2.3.1/gems/actionpack- 
    5.0.2/lib/action_dispatch/middleware/templates/rescues/ 
    _request_and_response.html.erb (1.7ms) 
Rendered /home/prikster/.rvm/gems/ruby-2.3.1/gems/actionpack- 
    5.0.2/lib/action_dispatch/middleware/templates/rescues/ 
    template_error.html.erb within rescues/layout (62.6ms) 

是在正確的模型accepts_nested_attributes_for

在日誌中有指向一個Unpermitted parameter: login線。我該怎麼做才能糾正這個問題?

+0

你能後,一旦你點擊提交出現日誌? – Pavan

+0

你也可以告訴我們創建方法嗎? – Pavan

+0

我添加了創建方法。正如你所看到的,它只是爲設計的創建方法調用super。 至於日誌,我在現在的工作,一旦我回家,我會後它。 –

回答

1

我解決我的問題。

我剛一required: false添加到登錄的使用用戶的關係。

我的登錄模式是現在這個樣子:

class Login < ApplicationRecord 
    belongs_to :user, required: false 
end 

我認爲問題是,當它試圖創建/保存登錄用戶沒有有ID呢。

最終創建了兩個模型,它們之間的關係以及它們都具有其正確值的屬性。

+0

是的。這就是爲什麼我會建議你閱讀以下內容:https://stackoverflow.com/questions/17767449/rails-4-0-with-devise-nested-attributes-unpermited-parameters和Devise Wiki: –

相關問題