Search code examples
ruby-on-railsbefore-filter

rails before_filter blocking create method from successful save


So I making a splash page and I've setup a redirect per this post. But it's messing up the app. When I've got the code below in place, the app doesn't save email adresses submitted by users. (When I comment out the redirect code, everything works fine.)

class ApplicationController < ActionController::Base
  protect_from_forgery
  include SessionsHelper

   before_filter(:except => :splash) do
     redirect_to root_path
   end

 end

Here's the controller. It's based on Railscast #124. So for the splash page, there no user signed in, which means that after the invitation saves, it should save and flash "Thank you, we will notify when we are ready." But when the filter above is in place, it just re-renders 'new'.

class InvitationsController < ApplicationController

  def new
    @invitation = Invitation.new
  end

  def create
    @invitation = Invitation.new(params[:invitation])
    @invitation.sender = current_user
    if @invitation.save
      if signed_in?
        UserMailer.invitation(@invitation).deliver
        flash[:notice] = "Thank you, invitation sent."
        redirect_to hunts_path      
      else
        flash[:notice] = "Thank you, we will notify when we are ready."
        redirect_to root_path  
      end
    else
      render :action => 'new'
    end
  end

end

Any ideas what I'm doing wrong?


Solution

  • Hmm, not sure, but you set the @invitation.sender to the current user, and after saving it you test for signed_in?. Could it be that the invitation cannot be saved if the user isn't signed in?

    Edit: Sorry, oversaw you said that everything works fine if you remove the redirect. I thinks this redirect prevents the other actions from doing anything (btw you are only allowed to redirect once).