添加字段以进行设计使用Rails 4进行注册 [英] Adding Fields To Devise Sign Up Using Rails 4

查看:56
本文介绍了添加字段以进行设计使用Rails 4进行注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是Rails的新手,所以我仍然掌握很多东西。

To start off, I'm new to rails, so I'm still getting the hang of things.

我正试图在Devise注册页面中添加两个字段,一个名字字段和一个姓氏字段。我已经进行了更改视图和控制器的尝试。它们看起来像这样:

I'm attempting to add two fields to the Devise sign up page, a first name field and a last name field. I've gone ahead and changed the view and the controller in order to attempt this. They look like this:

Controller:

Controller:

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" %>

但是,当我查看该页面时出现此错误:

However, I get this error when I go to view the page:

undefined method `fName_field' for #<ActionView::Helpers::FormBuilder:0x00000003c89940>

在哪里必须定义这些方法?还是会在其他地方自动定义它们?

Where must I define those methods? or are they automatically defined somewhere else?

感谢您的帮助。

推荐答案

使用 f.something,您可以在表单中定义字段的类型。尝试类似的

With "f.something" you can just define the type of the field in the form. Try something like

<%= f.text_field :lastName %>

这应该可行。

您可以在此处阅读有关可用字段类型的信息: http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for

You can read about the available types of fields here: http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for

这篇关于添加字段以进行设计使用Rails 4进行注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆