2011-08-15 29 views
3

不知道爲什麼我得到這個。我做了一堆閱讀,但我無法做出正面或反面的評論。406在這樣的基本控制器上不可接受

我的控制器:

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 

沒有什麼瘋狂那裏。它是一個多部分(圖像)表單提交...可能與此有關?

更新

一些IRB的東西:

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

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

第二個例子似乎正是它實際上返回,在URL無視我的額外PARAMS。

我也應該注意到我的edit重定向是相同的,它的工作原理是完美的。

更新2

爲了表明這種情況如何完全無助的是,我已經改變了我的控制器,以這樣的:

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 

我仍然得到這個:

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

路線

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_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" 
+0

查看http://stackoverflow.com/questions/1414446/weird-406-not-acceptable-error – jschorr

+0

我有。它沒有幫助。 – Trip

+0

自創建以來,我一直通過這個控制器發送params [:mail_type]幾個月,沒有任何問題。現在它不起作用。不知道會有什麼替代方案。 – Trip

回答

3

的MIME類型的請求是由文件擴展名傳入確定。

這裏的錯誤是以下行:

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

助手admin_emails_path不應該通過電子郵件的列表。這個收集路徑應該是自己的。當你傳入@emails對象時,它會嘗試將其編碼到URL中並注入一段時間,該時間段將像解析文件擴展名(URL解碼版本%23%3Cemail:0x109eb6360%3E)一樣分析。

變化從參考:

admin_emails_path(@emails) 

到:

admin_emails_path 

...你不會看到這些格式錯誤。

+1

天才溫菲爾德再次來襲! – Trip