3
votes

I've just setup my router to route category resources with :id and :alias:

resources :categories, param: 'id/:alias'

Now, my routes are setup correctly, for example 'show' becomes:

category GET    /categories/:id/:alias(.:format)     categories#show

The to_param method on Category:

def to_param
  { id: id, alias: name.parameterize }
end

When using link_to category, it throws this error:

  • No route matches {:controller=>"categories", :action=>"show", :id=>#< Category id: 2, name: "Buiten", description: "", ancestry: nil, created_at: "2014-04-25 16:13:11", updated_at: "2014-04-25 16:13:11">, :alias=>nil, :format=>nil} missing required keys: [:alias]

I want to use links like this:

= link_to category

I do NOT want to use links like this:

= link_to category_path( { id: category.id, alias: category.name.parameterize } )

Is this possible?

1
what it is showing in rails console app.category_path(id: 2, alias: "asas") did it throw error ?Rajarshi Das
your alias is nil that's why it was throwing errorRajarshi Das
you can verify my comment app.category_path(id: 2, alias: nil) now you see it will throw errorRajarshi Das
look at your post :alias=>nil, :format=>nil}Rajarshi Das
you can simply avoid alias in to_param and send it as a parameter of query like /categories/2?alias=anythingRajarshi Das

1 Answers

0
votes

you can simply solve the alias nil in this way

  def to_param
    "#{self.id}-#{self.alias}"
  end