简体   繁体   中英

Two forms for different controllers on same page in rails

UPDATE:


The problem below stems from having a before_filter which authenticates the user.


I have two forms on the same page, two different form_for's each with a different model. They are as follows and are both in the devise/sessions/new view:

%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'

When entering a new email into the betasignup form though, it doesn't save into the database as it does when I have the same form in the views/beta_sign_ups/new view. Why is this? Do I need to do something with one of the controllers or perhaps the routes?

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

The html output of the betasignups form:

<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>

And the output from the server logs after submitting to the betasignups form:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM