更新:
下面的问题源于有一个对用户进行身份验证的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 / new视图中具有相同的表单时。为什么是这样?我是否需要对其中一个控制器或路线做些什么?
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"=>"onebroadway@test.com"}, "commit"=>"Submit"}
Completed in 35ms
答案 0 :(得分:0)
你仍然可以使用before_filter,只需传递一些选项,这样它只会激活特定的动作,或者使用它替代skip_before_filter来表示你在表单中使用的任何控制器动作。