0

I am new to rails, and for the first time I am working with pgsql. The way my project is set up it requires that a song cannot be created with out an artist. To accomplish this I used nested routes so the id persists. My problem is that I am trying to create an index for all the artists, and songs regardless of the artist. But I get the error below in both cases:

No route matches {:action=>"edit", :controller=>"users", :id=>nil}

in my routes file:

resources :users do 
    resources :songs do 
      get 'approve', on: :member
      get 'decline', on: :member
    end
  end 

  resources :songs

in the users controller

def index
    @users = User.all 
  end


def edit
    @user = User.find(params[:id])
  end

In the songs controller

def index
        @songs = Song.all 
    end
    def create
        @song = current_user.songs.build(params[:song])
        if @song.save
            #Send confirmation email
            @song.submit
            flash[:success] = "Song created!"
            redirect_to user_path(current_user)
        else
            render 'new'
        end
    end
def edit
        @song = current_user.songs.find_by_id(params[:id])
  end

In the Users index view

<%= render @users %>

and the user partial view

<li>
    <%= link_to user.name, user %>
    <% if current_user.admin? && !current_user?(user) %>
        |   <%= link_to "delete", user, method: :delete,
                                        data: { confim: "You sure?" } %>
    <% end %>
</li>

Both the Users and Songs index view are similar so I think posting both is redundant.

And this is the output from my development.log file

Started GET "/users" for 127.0.0.1 at 2013-07-07 13:26:17 -0500
Processing by UsersController#index as HTML
  [1m[35mUser Load (0.3ms)[0m  SELECT "users".* FROM "users" 
  Rendered users/index.html.erb within layouts/application (0.6ms)
  Rendered layouts/_shim.html.erb (0.0ms)
  [1m[36mUser Load (0.5ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."remember_token" = 'PTlmu8452Oan4mX1SLHOnA' LIMIT 1[0m
  Rendered layouts/_header.html.erb (3.9ms)
Completed 500 Internal Server Error in 62ms

ActionController::RoutingError (No route matches {:action=>"edit", :controller=>"users", :id=>nil}):
  app/views/layouts/_header.html.erb:15:in `_app_views_layouts__header_html_erb___3568227543968708446_70195568870980'
  app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___4424773992888194134_70195568771540'


  Rendered /usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.8ms)

Does the index work differently with postgresql? Or am I making a mistake or missing something here? I have never had this problem with sqlite3 but like I said this is my first time working with pgsql. I need help, thanks!

  • The `index`es in the code above are *actions* on the *controllers* and are orthogonal to the indexes in your database (regardless of the engine). – johnsyweb Jul 07 '13 at 03:49
  • Ok @Johnsyweb, so why am I getting this error? I dont think i've done anything wrong here, so it should be working fine. – Jude Igboanugo Jul 07 '13 at 18:36
  • 1
    Simply read the dev log: the error is coming from a partial called _header on line 15 (app/views/layouts/_header.html.erb:15). Paste that one in if you need more help still. – Jordan Sitkin Jul 07 '13 at 21:32

1 Answers1

1

It seems like you are trying to link to edit page without :id parameter, witch is incorrect.

It should be routes to right :edit action with :id parameter like this,

<%= link_to "Edit user", edit_user_path(user.id) %>
Talgat Medetbekov
  • 3,741
  • 1
  • 19
  • 26