0

I've used a form successfully in embedded Ruby that looks like this:

<%= 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 %>

However when I tried to put this into Bootstrap I came up against problems. I then tried to make a simpler HTML form like this:

<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>

...but I came up against the same error message: No route matches [POST] "/users/1/comments/new/user/comments". I am using nested resources, and Rails routes in the terminal tells me that the user_comments path 'POST' equates to user/user_id/comments and the Create controller action.

In my HTML form if I enter action="user/comments" then the resulting path is "/users/1/comments/new/user/comments", if I enter action="comments" the path is "/comments", and if I enter "/" the path is "/". The path that I'm trying to reach is "user/comments" but there seems to be no way there through my HTML form.

I've racked my brains but I can't understand why this might be. Any help would be much appreciated! Thanks

ps. Routes look like this:

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
Robert
  • 109
  • 3
  • 13

1 Answers1

0

update routes.rb use collection instead of resources to get /users/comments like path

resources :users do
  collection do
    get 'comments'
  end
end
Sachin R
  • 11,606
  • 10
  • 35
  • 40