0

Ok so I have a rails app where user can create pins. Then they can comments those pins. What I what to do is to remove the controller name in pin url.

So instead of: http://localhost:3000/pins/name I have http://localhost:3000/name

**I did that by using this in my **config/routes.rb****

Rails.application.routes.draw do

    resources :pins, :only => [:index, :new, :create], :path => '' do
    resources :comments
    member do
    put 'upvote'
  end 
  end

But now, when I try to comment a pin I have this error:

wrong constant name 'pin name'

and the error come fom this lines from my comments_controller.rb:

  def load_commentable
    resource, id = request.path.split('/')[1, 2]
    @commentable = resource.singularize.classify.constantize.friendly.find(id)
  end

Any ideas how I could fix this ?

EDIT:

**rake routes** output:

pin_comments GET      /:pin_id/comments(.:format)            comments#index
                         POST     /:pin_id/comments(.:format)            comments#create
         new_pin_comment GET      /:pin_id/comments/new(.:format)        comments#new
        edit_pin_comment GET      /:pin_id/comments/:id/edit(.:format)   comments#edit
             pin_comment GET      /:pin_id/comments/:id(.:format)        comments#show
                         PATCH    /:pin_id/comments/:id(.:format)        comments#update
                         PUT      /:pin_id/comments/:id(.:format)        comments#update
                         DELETE   /:pin_id/comments/:id(.:format)        comments#destroy

2 Answers2

0

Use params (Hash with request parameters) instead of resource, id = request.path.split('/')[1, 2]. This should fix your second problem.

Maxim
  • 9,701
  • 5
  • 60
  • 108
0

I think you probably came from a php background or something like that, cause I used to think like that back then when I used php my self, but in rails you don't touch the URI or try to parse it or anything, that's the router's job, and if it reached that part of your code then the job was already done.

If you are using the pin's name as a url then you should either use a friendly_id gem, or set the to_param method of the model.

The id of the pin will always be in the params[:pin_id] because that's how it's named in the routes, and the id of the comment will be in the params[:id], mind the variable names in the route

/:pin_id/comments/:id

I'm not sure what you mean by the resource, but if you mean the model name, well you're in a pin's controller, so it's safe to assume it's a pin model, but if you want the name of the controller then you could access params[:controller]

Your load_commentable method would probably look like this after fixing everything

def load_commentable
  @commentable = Pin.find(params[:pin_id]).comments.where(id: params[:id])
end
Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89
  • You are absoluetly right. The problem was those lines. I made the change in my controller so now comments catch the friendly_id gem. Thanks. Btw, I get it to work without this part .comments.where(id: params[:id]) - it seems to be useless to me as rails make the job for most of the routing process as you said. – Jérémy Zaccherini Mar 03 '15 at 23:37