LLL
LLL

Reputation: 1155

Rails does not appear to hit my controller

When I go through this with debugger, it does no hit the controller.

I get the following error:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

i.e. @user is nil. @user is nil even if I set it to find the first one via @user = @Users.first

I want to access the user via domain.com/id

Routes.rb

match ':id' => 'user#show'

Model

class User < ActiveRecord::Base
   attr_accessible :userLink, :userName
end

Controller controllers/user_controller.rb

def Show
    @user= User.find_by_id params[:id]
    # Attempted this with User.first to see if param was broken    

    respond_to do |format|
       format.html
       format.json { render json: @user}
    end
end

View file name views/user/show.erb

<script type="text/javascript">
    <%=  @user.id %>
</script>

Upvotes: 0

Views: 430

Answers (3)

Nitin Jain
Nitin Jain

Reputation: 3083

use

match '/:id' => 'user#show'

but this is not a good approach as many of your routes will be disabled by that like if you have www.yourdomain.com/profile will also goes to your show action.

also def Show here Show should be show

Upvotes: 1

Jeet
Jeet

Reputation: 1380

You don't need to specify route like this

Just replace this

match ':id' => 'user#show'

with

resources :users, only: [:show]

This will generate default route for you for show method

Upvotes: 1

JaTo
JaTo

Reputation: 2832

You have a typo.

def Show in your controllers should be def show

Upvotes: 1

Related Questions