填写具有两个值的表单

时间:2015-01-23 13:43:10

标签: ruby-on-rails

我有user / registrations / new.html.haml

- title 'User Registration'

.vcentered  .signup-box
  - if @user.authorizations.any?    %h1 We just need a little more information...
  - else    %h1 We just need a little bit of information...

  = form_for @user, url: user_registration_path, html: { class: 'new-user form-default' } do |f|
   - if @user.name.blank?
    = f.text_field :name, class: class_with_errors(@user, :name),
                          title: title_with_errors(@user, :name),
                          placeholder: 'Full Name'

   - if @user.email.blank? || @user.errors.any?
    = f.text_field :email, class: class_with_errors(@user, :email),
                           title: title_with_errors(@user, :email),
                           placeholder: 'Email'

   - if @user.authorizations.empty?
    = f.password_field :password, class: class_with_errors(@user, :password),
                                  title: title_with_errors(@user, :password),
                                  placeholder: 'Password'

   = f.fields_for :profile do |p|
    = p.hidden_field :username, value: () 
    = p.text_field :place, class: class_with_errors(@user.profile, :place),
                           title: title_with_errors(@user.profile, :place),
                           id: 'profile-place',
                           placeholder: 'Type in your closest city...'
    = p.hidden_field :city_id, id: 'profile_city_id'
    .tip
     E.G. "New York, New York, United States" or "Barcelona, Cataluña, Spain"

    .note
     Ps - We need this information to show you data relevant to your location.
     We don't share it with anyone. If you cannot find your city, please type
     the largest city closest to you.

   = f.submit 'Sign up', class: 'submit'

由于某些关联问题,我想填写

的值
 p.hidden_field :username, value: () 

名称和身份。

例如,如果用户将名称填写为“姓名姓氏”且用户ID为“55”,则用户名应为NameSurname55。

请建议代码。提前致谢

我的用户控制器如下所示:

class Users::RegistrationsController < Devise::RegistrationsController
  layout 'land'
  #before_action :set_username

  def create
    params[:user][:profile_attributes].delete(:place)

    super do |resource|
      UserMailer.welcome(resource).deliver

      aroundyoga_user = User.find_by(email: 'aroundyoga@gmail.com')
      resource.follow!(aroundyoga_user) && aroundyoga_user.follow!(resource)

      RegistrationWorker.perform_async(resource.id)
    end
  end

  protected

    def after_sign_up_path_for(resource)
      welcome_path
    end

end

4 个答案:

答案 0 :(得分:2)

您的:username输入已隐藏,因此我假设用户无法修改用户名。然后你最好从表单中删除这个字段并在服务器端执行所有逻辑:

def create
  params[:user][:profile_attributes].delete(:place)

  super do |resource|
    # Next 2 lines
    resource.profile.username = resource.name.gsub(/\s+/, "") + resource.id.to_s
    resource.profile.save

    UserMailer.welcome(resource).deliver

    aroundyoga_user = User.find_by(email: 'aroundyoga@gmail.com')
    resource.follow!(aroundyoga_user) && aroundyoga_user.follow!(resource)

    RegistrationWorker.perform_async(resource.id)
  end
end

修改:根据您提供的信息更新代码。 @ pkrawat1解决方案更好。

答案 1 :(得分:2)

在您的个人资料模型中添加此内容

before_create :generate_username

def generate_username
  self.username = self.user.name.underscore + self.user.id.to_s
end

答案 2 :(得分:1)

我会避免向公众披露身份证明,但如果你坚持:

在控制器中从params创建对象就像写

一样简单
mo = MyObject.new params[my_object]

在您的控制器中。如果您需要更改某些属性,则只需执行以下操作:

mo.name = mo.name + mo.id

请记住,在这两种情况下,您都必须致电save以保留更改。

在您的情况下,最大的困难是如果用户尚未持久化(即,它是使用User.new创建的新对象),则尚未为其分配ID。但也有here

的答案

最简单的方法可能是:

class Profile < ActiveRecord::Base
  after_create :update_name

  def update_name
    name = name + id
    save
  end
end

答案 3 :(得分:1)

看起来你在后台使用了accepts_nested_attributes。

所以你可以在配置文件模型中使用回调,如:

after_create: update_profile_username

def update_profile_username
  username = user.name + user.id
  save
end
相关问题