我正在使用Rails 4和Devise 3.0.0,并且是使用这些新的强参数的新手。我使用迁移将first_name和last_name添加到User模型,然后将以下内容添加到基于Devise Wiki的application_controller.rb中。
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
before_filter :configure_permitted_parameters, if: :devise_controller?
protect_from_forgery with: :exception
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :email, :password, :password_confirmation) }
end
end
我的sign_up视图(/views/devise/registrations/new.html.erb)正在爆炸,出现此错误消息:
undefined method `first_name' for #<ActionView::Helpers::FormBuilder:0x007fdcbcbe2170>
以下是我的完整观点:
<div class="container">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<h2 class="form-signin-heading">Sign up</h2>
<%= f.first_name :first_name, :autofocus => true, :placeholder => 'Fist name', :class => "form-control" %>
<%= f.last_name :last_name, :placeholder => 'List name', :class => "form-control" %>
<%= f.email_field :email, :placeholder => 'Email address', :class=> "form-control" %>
<%= f.password_field :password, :placeholder=> 'Password', :class=> "form-control" %>
<%= f.password_field :password_confirmation, :placeholder=> 'Password confirmation', :class=> "form-control" %>
<%= f.submit "Sign up", :class => 'btn btn-lg btn-primary btn-block' %>
<div class="shared_links"><%= render "devise/shared/links" %></div>
<% end %>
</div>
如果我从注册#new表单中删除了:first_name和:last_name行,一切正常。
答案 0 :(得分:1)
我认为您打算使用f.text_field
:
# This:
<%= f.first_name :first_name, :autofocus => true, :placeholder => 'Fist name', :class => "form-control" %>
<%= f.last_name :last_name, :placeholder => 'List name', :class => "form-control" %>
# Should become:
<%= f.text_field :first_name, :autofocus => true, :placeholder => 'Fist name', :class => "form-control" %>
#^^^^^^^^^^
<%= f.text_field :last_name, :placeholder => 'List name', :class => "form-control" %>
#^^^^^^^^^^