2012-05-14 34 views
1

張貼在這裏是我的路線:Rails的路由 - 與途徑名稱相同

root :to          => 'sites#index' 
match 'sites'         => 'sites#index' 
match 'sites/:site'       => 'sites#show' 
match 'sites/:site/publish'     => 'sites#publish', :via => :get 
match 'sites/:site/publish'     => 'sites#push', :via => :put 
match 'sites/:site/:entity_type'    => 'entity#index' 
match 'sites/:site/:entity_type/new'   => 'entity#new', :via => :get 
match 'sites/:site/:entity_type/new'   => 'entity#create', :via => :put 
match 'sites/:site/:entity_type/:entity_name' => 'entity#edit', :via => :get 
match 'sites/:site/:entity_type/:entity_name' => 'entity#update', :via => :put 

我遇到的問題是,當我爲做POST發佈,它實際上並不在調用操作方法途徑所有。它指出,「entity_type_參數(這不應該被指定)設置爲‘發佈’

這是我的方式:

<%= form_tag({:controller => 'sites', :action => 'publish'}) do %> 
    <%= hidden_field_tag 'site', params[:site] %> 

    <%= submit_tag 'Publish' %> 
<% end %> 

其實,我不需要指定隱藏字段,因爲這是做如路線的結果,當我點擊「發佈」,這是發生了什麼:

Started POST "/sites/kieransenior/publish" for 127.0.0.1 at 2012-05-14 20:35:48 +0100 
Processing by EntityController#index as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"bCooYei5XTbfNv4MwXqrYAvBzazdcCZpHr7HufKPcxo=", "site"=>"kieransenior", "commit"=>"Publish", "entity_type"=>"publish"} 
Completed 500 Internal Server Error in 1ms 

HTML表單看起來像這樣(爲清楚起見):

<form accept-charset="UTF-8" action="/sites/kieransenior/publish" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="bCooYei5XTbfNv4MwXqrYAvBzazdcCZpHr7HufKPcxo=" /></div> 
    <input id="site" name="site" type="hidden" value="kieransenior" /> 

    <input name="commit" type="submit" value="Publish" /> 
</form> 

我做錯了什麼導致它發佈到錯誤的地方?它必須是我的路由,這是因爲形式是正確的。

編輯

推送控制器動作:

def push 
    respond_to do |format| 
     redirect_to :controller => 'sites', :action => 'show', :site => params[:site] 
    end 
end 

廢料以上,倘使我有我的大腦擰緊。看起來我在某處甩掉了redirect_to,並沒有刪除respond_to。哎呦。

回答

1

使用match,可以改爲(和你應該)使用您計劃使用而不是簡單地說matchgetpostput或任何動詞。

例如,你可以不是爲你指的是兩條路線做到這一點:

get 'sites/:site/publish'  => 'sites#publish' 
post 'sites/:site/publish' => 'sites#push' 

編輯

它看起來就像如果你得到一個錯誤406那麼你的職位因爲您沒有針對提交的內容類型的格式而被拒絕。

如果使用respond_to,通常你指定的內容類型以及如何爲每種類型的事情 - 這樣的事情:

respond_to do |format| 
    format.html 
    format.xml { render :xml => @people.to_xml } 
end 

在你的,似乎沒有是一樣的格式規範:

def push 
    respond_to do |format| 
     redirect_to :controller => 'sites', :action => 'show', :site => params[:site] 
    end 
end 

406錯誤通常意味着你已經提交了內容類型爲respond_to條款,但沒有在respond_to規定(例如該內容類型,發佈到/app/model.json,但沒有format.json子句)。在你的情況下,有沒有格式的子句,所以這將是爲什麼406

所以,除非你有特殊原因使用respond_to,我建議只刪除它現在和離開:

def push 
    redirect_to :controller => 'sites', :action => 'show', :site => params[:site] 
end 
+0

它使用這些,這是很好的,但重定向不起作用。該URL保持不變,但命令提示符指出以下內容:在2012-05-14 20:54:05 +0100開始POST「/ sites/kieransenior/publish」for 127.0.0.1#SitesController處理#push as HTML 參數:{「utf8」=>「✓」,「authenticity_token」=>「bCooYei5XTbfNv4MwXqrYAvBzazdcCZpHr7HufKPcxo =」,「site」=>「kieransenior」,「commit」=>「Publish」} 重定向到http:// localhost: 3000/sites/kieransenior 已完成406在9毫秒內不可接受(ActiveRecord:0.0ms) – Kezzer

+0

控制器對於'push'方法看起來像什麼?通常'406'意味着你的控制器出於某種原因拒絕了這個帖子。 –

+0

編輯原文。事實上,由於使用get/post而不是匹配,我現在無法使用其他表單上的更新。 – Kezzer

2
post 'sites/:site/publish'     => 'sites#publish' 
+0

通過什麼用的搭配呢?我一直認爲我這樣做的方式就是要完成它的方式。我被騙了! – Kezzer

相關問題