2
我想一個實例變量傳遞給一個佔位符的form_for:如何在Rails中將實例變量傳遞給form_form中的佔位符?
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :number %>
<em>(add 1 + area code w/ no spaces; i.e. 15555555555)</em><br/>
<%= f.text_field :phone, :placeholder => @phone_number.phone_number %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
當我這樣做,我得到「未定義的方法`PHONE_NUMBER」的零:NilClass。 @phone_number設置相同的控制器作爲視圖
...這裏是相應的控制器
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :configure_sign_up_params, only: [:create]
before_filter :configure_account_update_params, only: [:update]
#before_filter :update_sanitized_params, if: :devise_controller?
# GET /resource/sign_up
def new
@phone_number = PhoneNumber.find_or_create_by(phone_number: params[:phone_number][:phone_number])
redirect_to welcome_index_path
end
語法看起來正確,但'@ phone_number'由於某種原因是零。你可以發佈@ phone_number'設置的控制器嗎? – thebenedict
class PhoneNumbersController
ztredded
對不起,意外地打回來併發送錯誤的文本。這是通過設計設置我resgistrations控制器: – ztredded