pzin
pzin

Reputation: 4248

Changing the params in URL on rails

I want to change the :id param on the URL. I added to my routes.rb file something like:

match "articles/:name/edit", :to => 'articles#edit', :as => 'edit_article'

Thinking that :name would be readed by the server as params[:name] later for me in rails. I edited my article controller definition for edit so:

def edit
    @article = Article.find(params[:name])
end

I get always the error couldn't find article with id=test and I was wondering why "id" instead of :name? I tried also changing match to get but I got the same.

I have also the default resources :articles still in my routes.rb file, don't know if there's something like a double rule working there.

The whole thing is that instead of ID numbers I would use names in my URL —not just the edit one, with the show method I could handle it, but not with edit/update/delete.

I was reading about routing but I can't figure out what I am doing wrong.

Upvotes: 0

Views: 111

Answers (1)

apneadiving
apneadiving

Reputation: 115511

By default, find search by id.

You should replace it with find_by_name.

Advice: use friendly_id

Upvotes: 2

Related Questions