rugbert
rugbert

Reputation: 12673

Rails 3 resource routes missing paths

I dont know what Ive done with this new app but my routes are really messed up. I seem to be missing a lot of paths for my resources which is wreaking havoc on my app. There are a few controllers that seem to be routing to one another. Ill post my routes files and my rake routes to explain:

routes file

devise_for :users
root :to => "index#home"

#members section and its nested images
resources :members, :except => [:new, :create] do
    resources :tattoos
end

#admin section
namespace :admin do
  root :to => "admin#index"

  # pulls all users to manage
  resources :users do
    #pulls just admin/artist users
    collection do
      get 'admins'
      get 'artists'
    end
  end

  # Adds resources to manage (approve/reject) images
  resources :tattoos do
    # adds a couple extra actions on images
    collection do
      get 'reported'
      put "mass_approve"
    end
  end
end


#public tattoo viewing and submissions
match "/submit" => "index#new", :via => :get, :as => "submit"
match "/submit" => "index#create", :via => :post
match "/tattoo/:id" => "index#show", :via => :get, :as =>"tattoo"
match "/tagged" => "index#tagged", :via => :get
match "/tattoo/:id" => "index#destroy", :via => :delete
match "/tattoos" => "index#index", :via => :get, :as => "tattoos"

rake routes

                      root        /(.:format)                                    {:controller=>"index", :action=>"home"}
            member_tattoos GET    /members/:member_id/tattoos(.:format)          {:action=>"index", :controller=>"tattoos"}
                           POST   /members/:member_id/tattoos(.:format)          {:action=>"create", :controller=>"tattoos"}
         new_member_tattoo GET    /members/:member_id/tattoos/new(.:format)      {:action=>"new", :controller=>"tattoos"}
        edit_member_tattoo GET    /members/:member_id/tattoos/:id/edit(.:format) {:action=>"edit", :controller=>"tattoos"}
             member_tattoo GET    /members/:member_id/tattoos/:id(.:format)      {:action=>"show", :controller=>"tattoos"}
                           PUT    /members/:member_id/tattoos/:id(.:format)      {:action=>"update", :controller=>"tattoos"}
                           DELETE /members/:member_id/tattoos/:id(.:format)      {:action=>"destroy", :controller=>"tattoos"}
                   members GET    /members(.:format)                             {:action=>"index", :controller=>"members"}
               edit_member GET    /members/:id/edit(.:format)                    {:action=>"edit", :controller=>"members"}
                    member GET    /members/:id(.:format)                         {:action=>"show", :controller=>"members"}
                           PUT    /members/:id(.:format)                         {:action=>"update", :controller=>"members"}
                           DELETE /members/:id(.:format)                         {:action=>"destroy", :controller=>"members"}
                admin_root        /admin(.:format)                               {:controller=>"admin/admin", :action=>"index"}
        admins_admin_users GET    /admin/users/admins(.:format)                  {:action=>"admins", :controller=>"admin/users"}
       artists_admin_users GET    /admin/users/artists(.:format)                 {:action=>"artists", :controller=>"admin/users"}
               admin_users GET    /admin/users(.:format)                         {:action=>"index", :controller=>"admin/users"}
                           POST   /admin/users(.:format)                         {:action=>"create", :controller=>"admin/users"}
            new_admin_user GET    /admin/users/new(.:format)                     {:action=>"new", :controller=>"admin/users"}
           edit_admin_user GET    /admin/users/:id/edit(.:format)                {:action=>"edit", :controller=>"admin/users"}
                admin_user GET    /admin/users/:id(.:format)                     {:action=>"show", :controller=>"admin/users"}
                           PUT    /admin/users/:id(.:format)                     {:action=>"update", :controller=>"admin/users"}
                           DELETE /admin/users/:id(.:format)                     {:action=>"destroy", :controller=>"admin/users"}
    reported_admin_tattoos GET    /admin/tattoos/reported(.:format)              {:action=>"reported", :controller=>"admin/tattoos"}
mass_approve_admin_tattoos PUT    /admin/tattoos/mass_approve(.:format)          {:action=>"mass_approve", :controller=>"admin/tattoos"}
             admin_tattoos GET    /admin/tattoos(.:format)                       {:action=>"index", :controller=>"admin/tattoos"}
                           POST   /admin/tattoos(.:format)                       {:action=>"create", :controller=>"admin/tattoos"}
          new_admin_tattoo GET    /admin/tattoos/new(.:format)                   {:action=>"new", :controller=>"admin/tattoos"}
         edit_admin_tattoo GET    /admin/tattoos/:id/edit(.:format)              {:action=>"edit", :controller=>"admin/tattoos"}
              admin_tattoo GET    /admin/tattoos/:id(.:format)                   {:action=>"show", :controller=>"admin/tattoos"}
                           PUT    /admin/tattoos/:id(.:format)                   {:action=>"update", :controller=>"admin/tattoos"}
                           DELETE /admin/tattoos/:id(.:format)                   {:action=>"destroy", :controller=>"admin/tattoos"}
                    submit GET    /submit(.:format)                              {:controller=>"index", :action=>"new"}
                           POST   /submit(.:format)                              {:controller=>"index", :action=>"create"}
                    tattoo GET    /tattoo/:id(.:format)                          {:controller=>"index", :action=>"show"}
                    tagged GET    /tagged(.:format)                              {:controller=>"index", :action=>"tagged"}
                           DELETE /tattoo/:id(.:format)                          {:controller=>"index", :action=>"destroy"}
                   tattoos GET    /tattoos(.:format)                             {:controller=>"index", :action=>"index"}

Now, the problems Im having are stuff like, if I try to post new tattoos as a user(which is part of my nested resources) I am somehow routed to /tattoos (in my index controller) when I try to create the record, where I should be routed to members/member_id/tattoos. And if I remove that one line in my routes (match "/tattoos" => "index#index", :via => :get, :as => "tattoos) and try to go to /members/member_id/tattoos/new I get the follow error:

No route matches "/tattoos"

In fact, if I try to upload new photos from the /submit page I also get that error?

What gives? My routes are totally whack!

Requested Form code and controller code for good measure

    -- Forms:
    <div class="content">
      <%= form_for @tattoo, :html =>{:multipart => true} do |f| %>
       <ol>
          <li> blah blah blah

--members>tattoos_controller
class TattoosController < ApplicationController

  def new
    @tattoo = Tattoo.new
  end

  def create
    @tattoo = Tattoo.new(params[:image])

    if @tattoo.save
      flash[:success] = "Tattoo sent in for approval!"
      redirect_to(member_tattoos_path(current_user, @tattoo))
      else
      render :action => "new"
    end
  end

class IndexController < ApplicationController

  def new
    @tattoo =Tattoo.new
  end

  def create
    @tattoo =Tattoo.new(params[:image])

    if @tattoo.save
      flash[:success] = "Tattoo sent in for approval!"
      redirect_to(tattoos_path)
      else
      render :action => "new"
    end
  end

Upvotes: 1

Views: 2703

Answers (2)

Ryan Bigg
Ryan Bigg

Reputation: 107738

I think your routes are all flavours of messed up. Sorry, was not sure how to sugar coat that so I didn't.

Why are your routes called members? Why not call them users or rename the User model to be called Member instead?


Solution #1

Change your routes to this (simplified, adjust as necessary):

resources :users do
  resources :tattoos
end

Routes will then be /users/1/tattoos for example. You would then be able to use the form_for like this:

<%= form_for [current_user, @tattoo], :html => { :multipart => true } %>

Solution #2

Leave routes as is and rename User model to Member. You need to rename the model, the table, tell Devise that you want devise_for :members rather than devise_for :users in your config/routes.rb file and maybe some other things I am forgetting right now.


The whole point I am trying to get across: please try to have consistent in your code and the front-facing part of your application. You do not want to have your users referring to themselves as "members" while you are referring to them as "users". It is almost akin to speaking different languages for the sake of doing so. It will only lead to heart(ache|break).

Please, name things consistently everywhere.

Upvotes: 2

dogenpunk
dogenpunk

Reputation: 4382

Try this on your member_tattoos form

<%= form_for [current_user, @tattoo], :html => { :multi_part => true } do |f| %>

If you look at the generated form action on your member_tattoos page I bet it's '/tattoos' and not '/members/(whatever id)/tattoos'.

Upvotes: 1

Related Questions