Devise中的NoMethodError :: RegistrationsController #create undefined方法'sanitize'

时间:2015-01-27 17:46:40

标签: ruby-on-rails ruby devise oauth-2.0 omniauth

我对rails非常陌生,并感谢任何帮助。我无法让我的用户使用设计注册并获得:

Devise中的NoMethodError :: RegistrationsController #create 未定义的方法`sanitize'对于#

基于我的registrations_controller:

  def create
    build_resource(sign_up_params)

  def sign_up_params
    devise_parameter_sanitizer.sanitize(:sign_up)
  end

我使用了devise_parameter_sanitizer.for(:sign_up)而不是像MrYoshiji所建议的devise_parameter_sanitizer.sanitize(:sign_up)这样有效,但后来我得到了未定义的方法is_flashing_format?'。我把它更改为' is_navigational_format?'并且它工作了,现在我得到未定义的方法expire_data_after_sign_in!'。所以我继续得到未定义的方法。我认为这可能是因为我删除了受保护的属性gem,因为除非我这样做,否则我的facebook和linkedin的omniauth将无效。我该如何解决这个问题?

我的应用程序控制器如下所示:

class ApplicationController < ActionController::Base
 before_filter :configure_permitted_parameters, if: :devise_controller?
 protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) do |u|
        u.permit(:image, :first_name, :last_name, :address, :city, :state_province, :country, :password, :password_confirmation, :email, :position, :credentials, :headline, :description, :location_photo, :portfolio_photo, :study_style, :current_password)
    end
    devise_parameter_sanitizer.for(:account_update) do |u|
        u.permit(:image, :first_name, :last_name, :address, :city, :state_province, :country, :password, :password_confirmation, :email, :position, :credentials, :headline, :description, :location_photo, :portfolio_photo, :study_style, :current_password)
    end
   end

 def ensure_signup_complete
    # Ensure we don't go into an infinite loop
    return if action_name == 'finish_signup'

    # Redirect to the 'finish_signup' page if the user
    # email hasn't been verified yet
    if current_user && !current_user.email_verified?
      redirect_to finish_signup_path(current_user)
    end
  end


  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  private
  def not_authenticated
    redirect_to login_url, alert: "Please login to access this page"
  end
end

gemfile就像这样:

source 'https://rubygems.org'
ruby '2.1.3'

gem 'byebug', '~> 3.5.1'
gem 'rails', '4.0.3'
gem 'pg'
gem 'bootstrap-sass', '~> 3.1.1'
gem 'faker'
gem 'geocoder'
gem 'carrierwave'
gem 'mini_magick'
gem 'figaro'
gem 'state_machine'
gem 'jquery-ui-rails', '~> 4.2.1'
gem 'rails_12factor'
gem 'elasticsearch-model'
gem 'simple_form'
gem 'omniauth-facebook'
gem 'omniauth-linkedin'
gem 'omniauth'
gem 'pry-rails'
gem 'sidekiq'
gem 'will_paginate', '~> 3.0.6'
gem 'will_paginate-bootstrap'
gem "js-routes"
gem "mailchimp-api", require: 'mailchimp'
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'jquery-fileupload-rails'
gem 'jbuilder', '~> 1.2'
gem 'devise', '3.0.0.rc'
gem 'rails_12factor'



group :development, :test do
  gem 'rspec-rails', '2.13.1'
  gem 'quiet_assets'
  # gem 'pry-debugger'
  gem 'meta_request'
  gem 'interactive_editor'
  gem 'hirb'
  gem 'awesome_print'
  gem 'selenium-webdriver', '2.35.1'
  gem 'capybara', '2.1.0'
  gem 'factory_girl_rails', '4.2.1'
end
#
group :production do

end

group :doc do

  gem 'sdoc', require: false
end

我的用户控制器如下所示:

class UsersController < ApplicationController
  skip_before_filter :authenticate_user!, only: [:index,:new,:create,:activate]
  before_filter :authenticate_user!, only: [:edit,:update,:destroy]
  before_filter :admin_user, only: :destroy
  before_action :set_user, only: [:show, :edit, :update, :destroy]
  before_action :find_user, only: [:infowindow, :edit_meetup_info, :finish_signup]
  # respond_to :json, :html

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      flash[:success] = "Thank you for signing up! A confirmation email has been sent to your inbox"
      redirect_to users_path
    else
      render :new
    end
  end


  def edit
  @user = User.find(current_user.id)
  successfully_updated = if needs_password?(@user, params)
      @user.update_with_password(devise_parameter_sanitizer.sanitize(:account_update))
    else
      # remove the virtual current_password attribute
      # update_without_password doesn't know how to ignore it
      params[:user].delete(:current_password)
      @user.update_without_password(devise_parameter_sanitizer.sanitize(:account_update))
    end

    if successfully_updated
      set_flash_message :notice, :updated
      # Sign in the user bypassing validation in case their password changed
      sign_in @user, :bypass => true
      redirect_to after_update_path_for(@user)
    else
      render "edit"
    end
  end



  def update
    # authorize! :update, @user
    respond_to do |format|
      if @user.update(user_params)
        sign_in(@user == current_user ? @user : current_user, :bypass => true)
        format.html { redirect_to @user, notice: 'Your profile was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

    def finish_signup
    # authorize! :update, @user 
    if request.patch? && params[:user] #&& params[:user][:email]
      if @user.update(user_params)
        @user.skip_reconfirmation!
        sign_in(@user, :bypass => true)
        redirect_to @user, notice: 'Your profile was successfully updated.'
      else
        @show_errors = true
      end
    end
  end

  def destroy
    # authorize! :delete, @user
    @user.destroy
    respond_to do |format|
      format.html { redirect_to root_url }
      format.json { head :no_content }
    end
  end

  def activate
    if (@user = User.load_from_activation_token(params[:id]))
      @user.activate!
      redirect_to(login_path, :info => 'User was successfully activated.')
    else
      not_authenticated
    end
  end

  def infowindow
    respond_to do |format|
      format.html {render partial: "infowindow", locals: {user: @user}}
    end
  end

  def tooltip
    respond_to do |format|
      format.html {render partial: "tooltip"}
    end
  end

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

  def available
    # accept a params[:date]
    # user.available?(params[:date]) => true / false
  end

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

    def user_params
      accessible = [ :user_id, :first_name, :last_name, :email, :password, :password_confirmation, :gender, :address, :city, :state_province, :image, :country, :meetup_location_available, :current_online_learning, :languages, :location_photo, :portfolio_photo, :position, :meetup_location_available, :accommodates, :expertise ] # extend with your own params
      accessible << [ :password, :password_confirmation ] unless params[:user][:password].blank?
      params.require(:user).permit(:user_id, :first_name, :last_name, :email, :gender, :password, :password_confirmation, :address, :city, :state_province, :image, :country, :meetup_location_available, :current_online_learning, :languages,  :location_photo, :portfolio_photo, :position, :meetup_location_available, :accommodates, :expertise)
    end

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

    def admin_user
      redirect_to root_url unless current_user.admin?
    end
  end

感谢您的帮助

0 个答案:

没有答案