隐藏的属性不填充Rails 3.2表单中的字段
问题描述:
我正在实现一个邀请系统,我希望new user form
预先在表单上的电子邮件地址字段中填充用户的电子邮件地址(最终,我将重构此内容,而不是form_field),以便用户不必输入所有信息,只需输入密码即可。隐藏的属性不填充Rails 3.2表单中的字段
我已经创造了users.rb
模型中的getter/setter方法是这样的:
def invitation_token
invitation.invitation_token if invitation
end
def invitation_token=(invitation_token)
self.invitation = Invitation.find_by_invitation_token(invitation_token)
end
邀请MODEL
class Invitation < ActiveRecord::Base
#--== ASSOCIATIONS
belongs_to :sender, :class_name => 'User'
has_one :recipient, :class_name => 'User'
#--== CALLBACKS
before_create :generate_token
before_create :recipient_is_not_registered
before_create :decrement_sender_count, :if => :sender
#--== VALIDATIONS
validates_presence_of :recipient_email
#validate :recipient_is_not_registered
validate :sender_has_invitations, :if => :sender
#--== METHODS
private
def recipient_is_not_registered
if User.find_by_email(recipient_email)
false
else
true
end
end
def sender_has_invitations
unless sender.invitation_limit > 0
redirect_to root_url
end
end
def generate_token #TODO: MOVE to lib/generate_token.rb
self.invitation_token = Digest::SHA1.hexdigest([Time.now, rand].join)
end
def decrement_sender_count
sender.decrement! :invitation_limit
end
end
用户控制器
class UsersController < ApplicationController
def new
@user = User.new(:invitation_token => params[:invitation_token])
@user.email = @user.invitation.recipient_email if @user.invitation
end
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
redirect_to root_url, notice: "Thank you for signing up!"
else
render "new"
end
end
...
def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :admin)
end
end
组的意见/用户/ _form.html.erb
<%= form_for @user do |f| %>
<%= f.hidden_field :invitation_token %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="field">
<%= f.check_box :admin %>
<%= f.label :admin %>
</div>
<div class="actions"><%= f.submit %></div>
<% end %>
我下面Ryan Bates的RC#124 - 测试版邀请,并得到了困在这里。他的代码不会产生错误,所以我应该提到这是一个Rails 3.2.18应用程序。
当我重新加载表单时,用户的电子邮件未填充到表单中。相关日志显示:
Started GET "/signup.914823d28d07b747213ec3de47f89ad537169e34" for 127.0.0.1
at 2016-04-30 20:24:47 -0600
Processing by UsersController#new as
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."auth_token" = 'rOHiKmDcceytxi_t151YIQ' LIMIT 1
Invitation Load (0.0ms) SELECT "invitations".* FROM "invitations" WHERE "invitations"."invitation_token" IS NULL LIMIT 1
Rendered users/_form.html.erb (5.0ms)
Rendered users/new.html.erb within layouts/application (6.0ms)
Completed 200 OK in 102.0ms (Views: 25.0ms | ActiveRecord: 3.0ms)
所以看来invitation_token没有被传递,因为日志显示为NULL。
我已经从上到下浏览了RC代码,无法找出它未被传递的原因。
任何帮助,将不胜感激。谢谢。
UPDATE:从视图源的输出是:
<input id="user_invitation_token" name="user[invitation_token]" type="hidden" />
,所以它不是被传递沿。
答
通过将value:
键设置的隐藏字段中的值:
<%= f.hidden_field :invitation_token, value: some_value %>
练习的要点是有'invitation_token'从URL,例如填充:通过'http://本地主机:3000/signup/914823d28d07b747213ec3de47f89ad537169e34'这不是通过URL发送的,它通过电子邮件发送给用户。 – Matteo
您正在为'new'行为分配invitation_token给用户。那么为什么不'''?否则,怎么样''? –
这有效,但还有另一个问题。在我的代码中,它生成的URL为“http:// localhost:3000/signup.1b344bd1ed23ea367728239496339074fc859721”。这不起作用。 为了按预期进行填充工作,URL需要删除“。”。在“注册”之后并用“/”代替。我看不到它把这段时间放在斜线字符的位置上。 – Matteo