2017-09-13 37 views
0

我在嵌入的Ruby看起來像這樣使用的形式成功:路由Rails中形成

<%= form_for :comment, url: user_comments_path do |c| %> 
    <%= c.text_field :location %> 
    <%= c.text_field :title %> 
    <%= c.text_field :body %> 
    <%= c.submit ("Submit") %> 
<% end %> 

然而,當我試圖把這個變成引導我碰到了問題。然後我試圖做一個簡單的HTML表單是這樣的:

<form accept-charset="UTF-8" action="user/comments" method="post"> 
    <input id="comment_location" name="comment[location]" type="text"/> 
    <input id="comment_title" name="comment[title]" type="text"/> 
    <input id="comment_body" name="comment[body]" type="text"/> 
    <input name="commit" type="submit" value="Create"/> 
</form> 

...但我碰到了同樣的錯誤信息:沒有路由匹配[POST]「/用戶/ 1 /評論/新/用戶/註釋」。我正在使用嵌套資源,終端中的Rails路由告訴我,user_comments路徑'POST'等同於user/user_id/comments和Create controller操作。

在我的HTML表單中,如果我輸入action =「user/comments」,那麼結果路徑是「/ users/1/comments/new/user/comments」,如果我輸入action =「comments」 /評論「,如果我輸入」/「則路徑是」/「。我試圖達到的路徑是「用戶/評論」,但似乎沒有辦法通過我的HTML表單。

我絞盡腦汁,但我不明白爲什麼會這樣。任何幫助將非常感激!謝謝

ps。路線是這樣的:

Rails.application.routes.draw do 
    get 'sessions/new' 

    get 'welcome/index' 

    root 'welcome#index' 

    get 'user' => 'users#show' 

    get 'login', to: 'sessions#new' 
    post 'login', to: 'sessions#create' 
    delete 'logout', to: 'sessions#destroy' 

    get "log_out" => "sessions#destroy", :as => "log_out" 
    get "log_in" => "sessions#new", :as => "log_in" 
    get "sign_up" => "users#new", :as => "sign_up" 
    root :to => "users#new" 

    resources :users do 
    resources :comments 
    end 

    resources :sessions 

end 
+0

你可以上傳你的路由文件 – widjajayd

回答

0

更新routes.rb使用collection代替resources得到/users/comments像路徑

resources :users do 
    collection do 
    get 'comments' 
    end 
end