#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
models / user.rb简要说明
belongs_to :country
belongs_to :city
和city.rb
class City < ApplicationRecord
belongs_to :country
has_many :users
end
我不太明白错误在哪里..
答案 0 :(得分:0)
我认为在您允许的参数中,您在两个不同的表中添加一列对吗?在一个触发器?
尝试像这样转换代码
k
端
答案 1 :(得分:0)
尝试理解此代码
或者您可以将代码转换为此
我有用户模型和教师模型
我在用户登录时使用设计进行身份验证
这里是代码
<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 %>
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
以这种形式填写电子邮件,密码,名字和姓氏
电子邮件,密码用于用户型号和名字,姓氏用于教师型号
这是我的用户模型user.rb
class Teacher < ApplicationRecord
belongs_to :user
validates :firstname, uniqueness: {scope: :lastname}, presence: true
validates :lastname, presence: true
end
这是我的老师.rb
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
我的application_controller
org.wso2.carbon.user.core.ldap.ActiveDirectoryUserStoreManager
我认为我们有相似的用途,作为参考你可以评论,如果我有错误,我会接受它我也是新的ruby on rails
答案 2 :(得分:0)
确定..设计验证问题
我只是来自models / user.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>
并在show方法中添加此代码
<p>Country: <%= @user_country.name%></p>
<p>City: <%= @user_city.name%></p>
我认为它会对你有帮助。