2012-05-10 58 views
1

UPDATE:兩種形式爲同一頁上的不同的控制器在導軌


問題下面從具有對用戶進行認證一個的before_filter莖。


我在同一頁上有兩種形式,兩種不同的form_for各有不同的型號。他們是如下,都在色器件/會話/新觀點:

%h2 Sign in 
= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| 
    %div 
    = f.label :email 
    %br/ 
    = f.email_field :email 
    %div 
    = f.label :password 
    %br/ 
    = f.password_field :password 
    - if devise_mapping.rememberable? 
    %div 
     = f.check_box :remember_me 
     = f.label :remember_me 
    %div= f.submit "Sign in" 
= render :partial => "devise/shared/links" 

=form_for BetaSignUp.new do |f| 
    .field 
    = f.label :email, "Your Email:" 
    = f.text_field :email 
    .actions 
    = f.submit 'Submit' 

當進入一個新的電子郵件到betasignup形式,雖然,它不保存到數據庫時,我也有同樣因爲它在views/beta_sign_ups /新視圖中形成。爲什麼是這樣?我是否需要對控制器或路線之一進行操作?

beta_sign_ups_controller:

class BetaSignUpsController < ApplicationController 
    def new 
    @beta_sign_up = BetaSignUp.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @beta_sign_up } 
    end 
    end 

    def create 
    @beta_sign_up = BetaSignUp.new(params[:beta_sign_up]) 

    respond_to do |format| 
     if @beta_sign_up.save 
     format.html { redirect_to root_path, notice: 'Thank you for signing up. You will be notified when we expand the beta group.' } 
     format.json { render json: @beta_sign_up, status: :created, location: @beta_sign_up } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @beta_sign_up.errors, status: :unprocessable_entity } 
     end 
    end 
    end 
end 

的betasignups形式的HTML輸出:

<form accept-charset="UTF-8" action="/beta_sign_ups" class="new_beta_sign_up" id="new_beta_sign_up" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="authenticity_token" type="hidden" value="K4wlzIZ958PdaC91vV4NMu4PLm3TF+yVebN0ll2VuoI="></div> 
<div class="field"> 
<label for="beta_sign_up_email">Your Email:</label> 
<input id="beta_sign_up_email" name="beta_sign_up[email]" size="30" type="text"> 
</div> 
<div class="actions"> 
<input name="commit" type="submit" value="Submit"> 
</div> 
</form> 

,並從服務器日誌提交給betasignups形式後的輸出:

Started POST "/beta_sign_ups" for 127.0.0.1 at 2012-05-10 11:36:17 -0400 
    Processing by BetaSignUpsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"K4wlzIZ958PdaC91vV4NMu4PLm3TF+yVebN0ll2VuoI=", "beta_sign_up"=>{"email"=>"[email protected]"}, "commit"=>"Submit"} 
Completed in 35ms 
+0

什麼是第二種形式的html輸出?你有beta_sign_ups控制器嗎? – bcd

+0

編輯的問題包括這些。 – John

+0

啊,問題是有一個before_filter認證用戶 – John

回答

0

你仍然可以使用before_filter,只是傳遞一些選項,以便它只會激活對特定的動作進行評價,或者使用它替代skip_before_filter作爲你在表單中使用的任何控制器動作。

相關問題