5

मैं रेल पर्यावरण पर रूबी के लिए नया हूं और अधिकांश समस्याओं को हल करने में सक्षम हूं, लेकिन मैं नहीं ढूंढ पा रहा हूं अभी तक इस समस्या का समाधान।रेल 4 और डिवाइसेज - उपयोगकर्ता का नाम डेटाबेस में जोड़ा नहीं जाता है जब नया उपयोगकर्ता

संदर्भ प्रदान करने के लिए:

  • रेल (4.0.0)
  • का उपयोग करते हुए वार्डन (1.2.3)
  • का उपयोग वसीयत का उपयोग करना (3.0.0)
  • rolify का उपयोग करना (3.2 .0)
  • का उपयोग कैनकैन (1.6.10)

मेरे समस्या

जब मैं वसीयत के पंजीकरण के साथ एक नया उपयोगकर्ता sign_up। नाम: डेटाबेस में नाम फ़ील्ड नहीं जोड़ा जा रहा है। देखें सर्वर उत्पादन

उपयोगकर्ता मॉडल (एप्लिकेशन/मॉडल/user.rb)

: नाम attr_accessible में शामिल है।

class User < ActiveRecord::Base 
    rolify 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :confirmable, 
    # :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :role_ids, :as => :admin 
    attr_accessible :name, :email, :password, :password_confirmation, :remember_me 

end 

उपयोगकर्ता नियंत्रक (एप्लिकेशन/नियंत्रक/users_controller.rb)।

मैंने रेल 4 के साथ रखने के लिए पैराम्स श्वेतसूची को जोड़ा है, लेकिन इससे समस्या हल नहीं हुई।

class UsersController < ApplicationController 
    before_filter :authenticate_user! 

    def index 
    authorize! :index, @user, :message => 'Not authorized as an administrator.' 
    @users = User.all 
    end 

    def show 
    @user = User.find(params[:id]) 
    end 

    def update 
    authorize! :update, @user, :message => 'Not authorized as an administrator.' 
    @user = User.find(params[:id]) 
    if @user.update_attributes(params[:user], :as => :admin) 
     redirect_to users_path, :notice => "User updated." 
    else 
     redirect_to users_path, :alert => "Unable to update user." 
    end 
    end 

    def destroy 
    authorize! :destroy, @user, :message => 'Not authorized as an administrator.' 
    user = User.find(params[:id]) 
    unless user == current_user 
     user.destroy 
     redirect_to users_path, :notice => "User deleted." 
    else 
     redirect_to users_path, :notice => "Can't delete yourself." 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_user 
     @user = User.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def user_params 
     params.require(:user).permit(:name, :email, :password, :password_confirmation, :remember_me) 
    end 
end 

वसीयत के नए पंजीकरण दृश्य (एप्लिकेशन/विचारों/वसीयत/पंजीकरण/new.html.erb)

<h2>Sign up</h2> 
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'form-vertical' }) do |f| %> 
    <%= f.error_notification %> 
    <%= display_base_errors resource %> 
    <%= f.input :name, :autofocus => true %> 
    <%= f.input :email, :required => true %> 
    <%= f.input :password, :required => true %> 
    <%= f.input :password_confirmation, :required => true %> 
    <%= f.button :submit, 'Sign up', :class => 'btn-primary' %> 
<% end %> 
<%= render "devise/shared/links" %> 

आवेदन नियंत्रक (एप्लिकेशन/नियंत्रक/application_controller.rb)

मैंने मजबूत मानकों के संबंध में निर्देशों का पालन किया है, और Devise

से आलसी आदमी के दृष्टिकोण को शामिल किया है
class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 
    before_filter :configure_permitted_parameters, if: :devise_controller? 

    rescue_from CanCan::AccessDenied do |exception| 
    redirect_to root_path, :alert => exception.message 
    end 

    protected 

    def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:name, :email) } 
    end 
end 

नया उपयोगकर्ता बनाते समय सर्वर आउटपुट।

Started POST "/users" for 127.0.0.1 at 2013-07-16 15:31:20 +1000 
Processing by Devise::RegistrationsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"TYp9xOgtdKJI62rUddU7EE1C7FDF5qnmWgGENluzaWk=", "user"=>{"name"=>"John Smith", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"} 
Unpermitted parameters: name 
    (0.1ms) begin transaction 
    User Exists (0.1ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 
Binary data inserted for `string` type on column `encrypted_password` 
    SQL (0.3ms) INSERT INTO "users" ("created_at", "email", "encrypted_password", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Tue, 16 Jul 2013 05:31:20 UTC +00:00], ["email", "[email protected]"], ["encrypted_password", "$2a$10$kMfZLiBm6md0zoWXd0esjO/IRHBC72444ABDKcXVhPa6mCco9pIJu"], ["updated_at", Tue, 16 Jul 2013 05:31:20 UTC +00:00]] 
    (17.0ms) commit transaction 
    (0.1ms) begin transaction 
Binary data inserted for `string` type on column `last_sign_in_ip` 
Binary data inserted for `string` type on column `current_sign_in_ip` 
    SQL (0.4ms) UPDATE "users" SET "last_sign_in_at" = ?, "current_sign_in_at" = ?, "last_sign_in_ip" = ?, "current_sign_in_ip" = ?, "sign_in_count" = ?, "updated_at" = ? WHERE "users"."id" = 3 [["last_sign_in_at", Tue, 16 Jul 2013 05:31:20 UTC +00:00], ["current_sign_in_at", Tue, 16 Jul 2013 05:31:20 UTC +00:00], ["last_sign_in_ip", "127.0.0.1"], ["current_sign_in_ip", "127.0.0.1"], ["sign_in_count", 1], ["updated_at", Tue, 16 Jul 2013 05:31:20 UTC +00:00]] 
    (1.1ms) commit transaction 
Redirected to http://0.0.0.0:3000/ 
Completed 302 Found in 94ms (ActiveRecord: 19.0ms) 

मेरे निष्कर्ष

इस सब के बाद मेरा मानना ​​है कि समस्या में वसीयत के registration_controller निहित है, लेकिन मैं वास्तव में यकीन है कि नियंत्रक तक पहुँचने और इस सुधार के बारे में जाने के लिए कैसे, या यदि नहीं कर रहा हूँ यह वह जगह है जहां वास्तव में समस्या है। मुझे उम्मीद है कि यह कुछ आसान है और मैंने इसे अभी अनदेखा कर दिया है।

यदि कोई इस समस्या में भाग लेता है या स्थिति पर कुछ प्रकाश डाल सकता है, तो इसकी सराहना की जाएगी।

चीयर्स।

bundle open devise 

यदि आप मानक वसीयत नियंत्रक ओवरराइड करने के लिए की जरूरत है, तो आप से नियंत्रक को कॉपी करके ऐसा कर सकते हैं:

+1

आप मजबूत पैरा और attr_accessible दोनों का उपयोग क्यों करते हैं? –

+0

मैंने मूल ऐप बनाने के लिए स्टार्टरैप जनरेटर का उपयोग किया, इसमें attr_accessible लाइनें शामिल थीं, और रेल के लिए नया होने के कारण, अब तक मुझे कोई बेहतर जानकारी नहीं मिली - इस समस्या के माध्यम से मजबूत पैरामीटर के बारे में सीखा। अंतर्दृष्टि के लिए – 7sunami

उत्तर

10

रेल 4 मजबूत पैरामीटर का उपयोग करता है।

आपको उन पैरामीटरों को अनुमति देना है जिन्हें आप नियंत्रक के माध्यम से मॉडल में पास करना चाहते हैं। दूसरे शब्दों में पैराम्स के माध्यम से मॉडल में विशेषताओं के लिए सीधे सहयोग की अनुमति नहीं है, अब आपको यह निर्दिष्ट करने की अनुमति नहीं है कि क्या कोई पैरामीटर मान मॉडल के नियंत्रक के माध्यम से गुज़रने का अनुमान लगाता है।

आप इस ब्लॉग पर कुछ मदद मिल गया हो सकता है: http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html

या के रूप में यदि हम आपकी समस्या आप या तो वसीयत के लिए SessionsController ओवरराइड या

devise_parameter_sanitizer(:sign_up) 

अधिक मदद का उपयोग कर सकते यहां पाया जा सकता पर विचार करें: https://github.com/plataformatec/devise#strong-parameters

2

आप बंडल का उपयोग कर रहे मान लिया जाये, तो आप से कोड को देखने के लिए चिंतन करना मणि उपयोग कर सकते हैं आपके आवेदन में नियंत्रक/devise/[फ़ाइल नाम] में मणि और यह प्राथमिकता ले जाएगा। ध्यान दें कि यदि आप ऐसा करते हैं तो आपको मणि उन्नयन से सावधान रहना होगा।

पहले टिप्पणी के अनुसार, आप मजबूत पैरामीटर के साथ सफेद लिस्टिंग क्यों मिला रहे हैं; आप केवल रेल 4 में मजबूत मानकों की जरूरत है और 3

+0

धन्यवाद। मैंने देखा है लेकिन मैं एक पूर्ण ओवरराइड नहीं करना चाहता, जैसा कि आप कहते हैं, यह मणि को अपग्रेड करते समय समस्याग्रस्त हो सकता है। – 7sunami

0

मैं application_controller में समस्या पाया वसीयत चाहिए:

devise_parameter_sanitizer.for(:sign_in) ... 

जरूरतों

devise_parameter_sanitizer.for(:sign_up)... 

मेरी ओर से मामूली निरीक्षण, के लिए धन्यवाद करने के लिए हालांकि मदद करें।

8

जोड़ना

before_action :configure_permitted_parameters, if: :devise_controller? 

def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:name, :email) } 
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :email, :password, :password_confirmation) } 
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :email, :password, :password_confirmation, :current_password) } 
end 
,210 मेरी application_controller को

के लिए मुझे

अद्यतन

before_action :configure_permitted_parameters, if: :devise_controller? 

def configure_permitted_parameters 
    devise_parameter_sanitizer.permit(:sign_in, keys: [:name, :email]) 
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :email, :password, :password_confirmation]) 
    devise_parameter_sanitizer.permit(:account_update, keys: [:name, :email, :password, :password_confirmation, :current_password]) 
end 

यह वसीयत अद्यतन उनके दस्तावेजों की तरह लग रहा काम किया है और वे इसके बाद के संस्करण की तरह यह सेटअप करना चाहते हैं।

+0

: ईमेल: पासवर्ड होना चाहिए: ईमेल,: पासवर्ड –

+0

धन्यवाद @ रिचर्ड ग्रॉसमैन, तय। – codermyles

संबंधित मुद्दे