2016-09-15 68 views
0

enter image description here爲什麼Devise在保存用戶時顯示這些錯誤?

#view/devise/registrations/new.html.erb 


    <div class="authform"> 
    <%= resource%> 
    <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :role => 'form'}) do |f| %> 
    <h3>Sign up</h3> 
    <%= devise_error_messages! %> 

     <div class="form-group"> 
      <%= f.label :avatar, class: 'col-sm-2 control-label' %> 
      <div class="col-sm-6"> 
       <%= f.file_field :avatar %> 
      </div> 
     </div> 

     <br> 
    <div class="form-group"> 
     <%= f.label :Имя %> 
     <%= f.text_field :first_name, :autofocus => true, class: 'form-control' %> 
    </div> 
    <div class="form-group"> 
     <%= f.label :Фамилия %> 
     <%= f.text_field :last_name, :autofocus => true, class: 'form-control' %> 
    </div> 


    <div class="form-group"> 
     <%= f.label :Страна %> 
     <%= collection_select(:city, :country_id, Country.all, :id, :name) %> 
    </div> 

    <div class="form-group"> 
     <%= f.label :Город %> 
    <%= collection_select(f, :city_id, City.all, :id, :name) %> 
    </div> 

    <div class="form-group"> 
     <%= f.label :email %> 
     <%= f.email_field :email, class: 'form-control' %> 
    </div> 
    <div class="form-group"> 
     <%= f.label :Пароль %> 
     <%= f.password_field :password, class: 'form-control' %> 
    </div> 
    <div class="form-group"> 
     <%= f.label :password_confirmation %> 
     <%= f.password_field :password_confirmation, class: 'form-control' %> 
    </div> 
    <%= f.submit 'Sign up', :class => 'button right' %> 
    <% end %> 
</div> 

<%= render "devise/shared/links" %> 

這裏我想作一個輸入表單city_id但無一例外的舊版本(可以從註釋掉該行看到)我介紹了他們爲字符串,現在我得到的錯誤。 。

#application_controller.rb 
class ApplicationController < ActionController::Base 
    protect_from_forgery with: :exception 
    #force_ssl 
    before_action :configure_permitted_parameters, if: :devise_controller? 

    protected 

    def configure_permitted_parameters 
    added_attrs = [:first_name, :last_name, :email, :avatar,:conversation_params, 
      :city_id, :message_params, :password, :password_confirmation, :remember_me] 
    devise_parameter_sanitizer.permit :sign_up, keys: added_attrs 
    devise_parameter_sanitizer.permit :account_update, keys: added_attrs 
    end 

    def after_sign_in_path_for(resource) 
    request.env['omniauth.origin'] || stored_location_for(resource) || user_path(resource) 
    end 
end 

以及城市和國家,這種遷移文件

# migration file 
class AddCounriesAndCitiesToUser < ActiveRecord::Migration[5.0] 
    def change 
    add_column :users, :city_id, :integer 

    create_table :cities do |t| 
     t.string :name 
     t.integer :country_id 
    end 
    create_table :countries do |t| 
     t.string :name 
    end 
    end 
end 

型號/ user.rb簡要

belongs_to :country 
    belongs_to :city 

和city.rb

class City < ApplicationRecord 
    belongs_to :country 
    has_many :users 
end 

我不太明白的地方可以將誤差..

回答

0

好吧..用色器件問題驗證

我剛剛刪除這個從模型/用戶。RB

belongs_to :country 
    belongs_to :city 

,並將其添加到控制器

class UsersController < ApplicationController 
    before_filter :authenticate_user!, exception: [:show, :index] 
    protect_from_forgery 

    def index 
    user_up 
    end 

    def show 
    user_up 
    @user = User.find(params[:id]) 
    @user_city = City.find_by id: @user.city_id 
    @user_country = Country.find_by id: @user_city.country_id 
    end 

private 

    def user_up 
    @users = User.all 
    @city = City.all 
    @country = Country.all 
    end 

end 

,並鑑於

<div class="form-group"> 
    <%= f.label :City %> 
    <%= f.collection_select(:city_id, City.all, :id, :name) %> 
</div> 

,並在顯示方法更新選擇添加此代碼

<p>Country: <%= @user_country.name%></p> 
    <p>City: <%= @user_city.name%></p> 

我認爲它幫助您。

0

我想在你的允許使用的參數你在兩個不同的表右側添加一列?在一個觸發器?

嘗試將代碼轉換這樣

def configure_permitted_parameters 
devise_parameter_sanitizer.permit(:sign_up , keys: [:email, :password, :remember_me, :city, :country ]) 
devise_parameter_sanitizer.permit(:sign_in , keys: [:email, :password, :remember_me]) 
devise_parameter_sanitizer.permit(:account_update , keys: [:email, :password, :password_confirmation, :current_password ]) 

+0

不起作用。我認爲 代碼:##(DEF configure_permitted_pa​​rameters added_attrs = [:如first_name,:姓氏,:電子郵件,:頭像,:conversation_params, :city_id,:message_params,:密碼,:password_confirmation,:remember_me] devise_parameter_sanitizer.permit :sign_up,鍵:added_attrs devise_parameter_sanitizer.permit:account_update的,鍵:added_attrs 完) 相同 –

+0

我只是想澄清一下,當你填補了new.html.erb它會去到模型用戶,然後?到城市模型的權利? – wiwit

0

試着去了解這段代碼

,或者您可以將您的代碼轉換爲這個

我有用戶模式和教師模型

我使用devise進行身份驗證w在這裏

母雞用戶登錄的代碼

<h2>Sign up</h2> 
    <% resource.build_teacher if resource.teacher.nil? %> 
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> 
<div class="field"> 
     <%= teacher_fields.text_field :firstname, placeholder: 'First Name' %> 
     <%= teacher_fields.text_field :lastname, placeholder: 'Last Name' %> 
    </div> 
<% end %> 

<div class="field"> 
<%= f.label :email %><br /> 
<%= f.email_field :email, autofocus: true %> 
</div> 

<div class="field"> 
<%= f.label :password %> 
<% if @minimum_password_length %> 
<em>(<%= @minimum_password_length %> characters minimum)</em> 
<% end %><br /> 
<%= f.password_field :password, autocomplete: "off" %> 
</div> 

<div class="field"> 
<%= f.label :password_confirmation %><br /> 
<%= f.password_field :password_confirmation, autocomplete: "off" %> 
</div> 
<div class="actions"> 
<%# raise params %> 
<%= f.submit 'Sign up' %> 
</div> 

<% end %> 
以這種形式

我填寫了電子郵件,密碼,名字和姓氏

電子郵件,密碼是用戶模型和名字,姓氏是教師模型

這是我的用戶模型user.rb

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

has_one :teacher, dependent: :destroy 
accepts_nested_attributes_for :teacher 
end 

,這是我teacher.rb

class Teacher < ApplicationRecord 
belongs_to :user 
validates :firstname, uniqueness: {scope: :lastname}, presence: true 
validates :lastname, presence: true 
end 

我application_controller

class ApplicationController < ActionController::Base 
protect_from_forgery with: :exception 
before_action :authenticate_user! 
protect_from_forgery with: :exception 
before_action :configure_permitted_parameters, if: :devise_controller? 


def new 
# Override Devise default behaviour and create a profile as well 
build_resource({}) 
resource.build_teacher 
respond_with self.resource 
end 

protected 

def configure_permitted_parameters 
devise_parameter_sanitizer.permit(:sign_up , keys: [:email, :password, :remember_me, :teacher_attributes => [:user_id, :firstname, :lastname]]) 
devise_parameter_sanitizer.permit(:sign_in , keys: [:email, :password, :remember_me]) 
devise_parameter_sanitizer.permit(:account_update , keys: [:email, :password, :password_confirmation, :current_password ]) 

end 

end 

我認爲我們有相似性使用能夠爲你參考可以評論,如果我有錯誤,我會接受的即時通訊也有新的Ruby on Rails的

+0

由同一用戶編寫多個答案是不合適的。在同一個問題上發佈多個答案的唯一情況可能是適當的,即每個答案本身可以成爲問題的有效和完整的答案。你應該編輯你的答案來更新它。 – webster

+0

感謝提醒我<3 – wiwit

相關問題