首先,我是 Rails 新手,所以我仍然掌握了窍门。
我正在尝试将两个字段添加到设计注册页面,一个名字字段和一个姓氏字段。我已经继续并更改了视图和控制器以尝试此操作。它们看起来像这样:
控制器:
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_action :configure_devise_permitted_parameters, if: :devise_controller?
protected
def configure_devise_permitted_parameters
registration_params = [:firstName, :lastName, :email, :password, :password_confirmation]
if params[:action] == 'update'
devise_parameter_sanitizer.for(:account_update) {
|u| u.permit(registration_params << :current_password)
}
elsif params[:action] == 'create'
devise_parameter_sanitizer.for(:sign_up) {
|u| u.permit(registration_params)
}
end
end
end
看法:
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.label :firstName %><br />
<%= f.fName_field :firstName %></div>
<div><%= f.label :lastName %><br />
<%= f.lName_field :lastName %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>
但是,当我查看页面时出现此错误:
undefined method `fName_field' for #<ActionView::Helpers::FormBuilder:0x00000003c89940>
我必须在哪里定义这些方法?还是它们在其他地方自动定义?
谢谢你的帮助。