4

Not sure why I'm getting this. I did a bunch of reading and I can't make heads or tails of this.

My controller:

def create
  @emails = Email.new(params[:email])

  respond_to do |format|
    if @emails.save
      flash[:notice] = 'Email was successfully created.'
      format.html { redirect_to admin_emails_path(:mail_type => @emails.mail_type) }
      format.xml  { render :xml => @emails, :status => :created, :location => @emails }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @emails.errors, :status => :unprocessable_entity }
    end
  end
end

Nothing crazy there. Its a multipart(images) form submission..maybe that has something to do with?

Update

Some irb stuff:

>> admin_emails_path(:mail_type => @emails.mail_type)
"/admin/emails?mail_type=magic_email"

>> admin_emails_path(@emails)
"/admin/emails.%23%3Cemail:0x109eb6360%3E"

The second example seems to be what it actually is returning, ignoring my additional params in the URL.

I should also note that my edit redirect is identical, and it works perfectly.

Update 2

Just to show how completely helpless this situation is, I've changed my controller to this :

  if @emails.save
    flash[:notice] = 'Email was successfully created.'
    debugger
    format.html { render :action => "new" }      # <=== WTF ?
    format.xml  { render :xml => @emails, :status => :created, :location => @emails }
  else

And I still get this:

Completed in 7401ms (View: 3, DB: 7) | 406 Not Acceptable [http://localhost/admin/emails.%23%3Cemail:0x109fd2a28%3E]

Routes

admin.resources :emails, :collection => {:test_email => :get}, :member => {:update_current => :get, :send_email => :get, :duplicate => :get} do |email|
  email.resources :distributions, :collection => {:delete_dist => :get}
end

Form

- form_for @emails, :url => admin_email_path(@emails), :id => "email_form", :html => {:multipart => true} do |f|

... lots of stuff  ..

  .clear
%p
  = f.submit 'Save Email', :class => "button"
Trip
  • 26,756
  • 46
  • 158
  • 277
  • See http://stackoverflow.com/questions/1414446/weird-406-not-acceptable-error – jschorr Aug 15 '11 at 17:25
  • I've been sending the params[:mail_type] through this controller for months since its creation with no problems. now it doesn't work. not sure what an alternative would be. – Trip Aug 15 '11 at 17:29
  • What's more if I just make this a regular redirect using no params, it still returns the the same 406. – Trip Aug 15 '11 at 17:32
  • Post the console log for this request. – Maurício Linhares Aug 15 '11 at 17:33
  • The log: `Completed in 242ms (View: 1, DB: 7) | 406 Not Acceptable [http://localhost/admin/emails.%23%3Cemail:0x109fa3b88%3E]` – Trip Aug 15 '11 at 17:34
  • Never hurts to throw ruyb-debug in and look what rails is doing http://pivotallabs.com/users/chad/blog/articles/366-ruby-debug-in-30-seconds-we-don-t-need-no-stinkin-gui- – Scott Schulthess Aug 15 '11 at 17:51
  • @scott, i appreciate the suggestion, but there's nothing ruby-debug can show you for a 406 – Trip Aug 15 '11 at 17:57
  • 2
    and show your log before an error. – fl00r Aug 15 '11 at 18:47
  • Please add relevant dev log and relevant rake routes. – Paulo Abreu Aug 16 '11 at 08:08
  • Well, I agree it might be helpful to see the logs, but this problem could benefit from some more distillation. For now, remove the respond block and sub with just the redirect. Also, in your debug statement there, what is the output of the named route method as you have it in the controller? What is the value of @emails and @emails.mail_type? – voxobscuro Aug 17 '11 at 16:18
  • Yah just a redirect actually works without the respond_to. That's what I have now. – Trip Aug 17 '11 at 16:24

1 Answers1

3

The MIME type for the request is determined by the file extension incoming.

The error here is the following line:

>> admin_emails_path(@emails)
"/admin/emails.%23%3Cemail:0x109eb6360%3E"

The helper admin_emails_path should not be passed the list of e-mails. This collection path should work on it's own. When you pass in the @emails object, it's trying to encode it into the URL and injecting a period, which rails is parsing like a file extension (the url decoded version of %23%3Cemail:0x109eb6360%3E).

Change the reference from:

admin_emails_path(@emails)

to:

admin_emails_path

...and you will not see these format errors.

Winfield
  • 18,985
  • 3
  • 52
  • 65