5

私はRuby on Rails環境に新しく、私が解決できた多くの問題が見つかりましたが、この問題の解決策はまだありません。コンテキストを提供するためにRails 4&Devise - 新規ユーザー作成時にユーザー名がデータベースに追加されない

rolify使用考案使用ワーデン(1.2.3)

  • を使用して、レール(4.0.0)
  • (3.0.0)
  • (3.2を使用

    • .0)
    • カンカン(1.6.10)の使用

    私の問題

    私はと新しいユーザーsign_upをDevitsの登録します。 :nameフィールドはデータベースに追加されていません。名前はattr_accessibleに含まれている:サーバー出力

    Userモデル(アプリ/モデル/ user.rb)

    を参照してください。

    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 
    

    ユーザコントローラ(APP /コントローラ/ users_controller.rb)。

    Rails 4を維持するためにparamsホワイトリストを追加しましたが、これで問題は解決されませんでした。

    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

    基本的なアプリケーションを作成するためにstarterappジェネレータを使用しました。これはattr_accessibleのラインを含んでいました。これまでレールを初めて使っていました。 – 7sunami

    答えて

    10

    Rails 4は強力なパラメータを使用します。

    コントローラーをモデルに渡すパラメーターを許可する必要があります。 つまり、paramsを介したモデルの属性への直接の関連付けが許可されていないため、コントローラにモデルを渡すパラメータ値があるかどうかを指定する必要はありません。

    あなたはこのブログでいくつかの助けを見つけたことがあります。 http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html

    か、我々はここで見つけることができ、あなたの問題あなたは工夫のためSessionsControllerを上書きするか、

    devise_parameter_sanitizer(:sign_up) 
    

    より多くのヘルプを使用するかを検討しているかのよう。 https://github.com/plataformatec/devise#strong-parameters

    2

    gemをあなたのアプリケーションのコントローラ/ devise/[ファイル名]に入れてください。それが優先されます。 これを行う場合、宝石のアップグレードには注意が必要です。

    これまでのコメントによると、なぜ強力なパラメータでホワイトリストを混ぜているのですか?以下のためのおかげで、

    devise_parameter_sanitizer.for(:sign_in) ... 
    

    ニーズ私に代わって

    devise_parameter_sanitizer.for(:sign_up)... 
    

    わずかな監督であることを:私はapplication_controllerで問題が見つかっただけで、レール4に強いパラメータを必要とし、3

    +0

    洞察に感謝します。私は一見しましたが、私は完全なオーバーライドをしたくないと思います。あなたが言うように、それは宝石をアップグレードするときに問題になる可能性があります。 – 7sunami

    0

    を考案する必要があります助けてください。

    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 
    

    私application_controllerにUPDATE

    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

    関連する問題