2016-05-01 163 views
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" />,所以它不是被傳遞沿。

回答

2

通過將value:鍵設置的隱藏字段中的值:

<%= f.hidden_field :invitation_token, value: some_value %> 
+0

練習的要點是有'invitation_token'從URL,例如填充:通過'http://本地主機:3000/signup/914823d28d07b747213ec3de47f89ad537169e34'這不是通過URL發送的,它通過電子郵件發送給用戶。 – Matteo

+0

您正在爲'new'行爲分配invitation_token給用戶。那麼爲什麼不''<%= f.hidden_​​field:invitation_token,值:@ user.invitation_token%>'?否則,怎麼樣'<%= f.hidden_​​field:invitation_token,值:params [:invitation_token]%>'? –

+0

這有效,但還有另一個問題。在我的代碼中,它生成的URL爲「http:// localhost:3000/signup.1b344bd1ed23ea367728239496339074fc859721」。這不起作用。 爲了按預期進行填充工作,URL需要刪除「。」。在「註冊」之後並用「/」代替。我看不到它把這段時間放在斜線字符的位置上。 – Matteo

相關問題