#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
我不太明白的地方可以將誤差..
不起作用。我認爲 代碼:##(DEF configure_permitted_parameters 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 完) 相同 –
我只是想澄清一下,當你填補了new.html.erb它會去到模型用戶,然後?到城市模型的權利? – wiwit