添加新字段会导致错误

时间:2017-04-09 13:51:37

标签: ruby-on-rails ruby twitter-bootstrap

在我的注册页面中,我有2个字段,包含电子邮件和密码,现在我尝试添加带有名称的新字段,因此要归档/app/views/devise/registrations/new.html.erb/app/views/devise/registrations/edit.html.erb我已添加

<div class="form-group">
<%= f.label :name, "Name" %>
<%= f.text_field :name, class: "form-control", :autofocus => true %>
</div>

在此编辑后,我的网站显示此错误:

 Rendering devise/registrations/new.html.erb within layouts/application

我的new.html.erb

<div class="panel panel-default">
  <div class="panel-heading"><h2>Registration</h2></div>
    <div class="panel-body">


<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

          <div class="form-group">
            <%= f.label :name, "Name" %>
            <%= f.text_field :name, class: 'form-control', :autofocus => true %>
          </div>



          <div class="form-group">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true, class: 'form-control' %>
  </div>



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

  <div class="actions">
    <%= f.submit "Sign up", class: 'btn btn-primary' %>
  </div>
<% end %>
    </div>
      <div class="panel-footer"><%= render "devise/shared/links" %></div>

    </div>

我在某处犯了错误吗?

__修改

重新启动heroku后,我收到新错误:

Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)
app/controllers/application_controller.rb:11:in `configure_permitted_parameters'

我的:/app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_filter :configure_permitted_parameters, if: :devise_controller?


  protected


  def configure_permitted_parameters

    devise_parameter_sanitizer.for(:sign_up) << :name

    devise_parameter_sanitizer.for(:account_update) << :name

  end
end

2 个答案:

答案 0 :(得分:2)

试试这个,这会对你有帮助。

 class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    added_attrs = [:username, :email, :password, :password_confirmation, :remember_me]
    devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
    devise_parameter_sanitizer.permit :account_update, keys: added_attrs
  end
end

或点击此链接follow

答案 1 :(得分:1)

试试这个:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password,:name) }
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password,:name) }
  end
end

来自here

希望这有助于..