1

更新:


下面的问题源于有一个对用户进行身份验证的 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
4

1 回答 1

0

您仍然可以使用 before_filter,只需将一些选项传递给它,以便它只会在特定操作上激活,或者使用它替代 skip_before_filter 用于您在表单中使用的任何控制器操作。

于 2012-05-10T17:22:32.430 回答