Rails for easy routes and unique resources

I have a problem with quiet routes and unique resources, initially I had this code in the show view on my account resource.

<%= link_to book.title, book_path(:search => book.title) %>

and it worked fine, then I changed the account as a special resource, for example

from

map.resources :accounts

to

map.resource :account

and now I get an error ...

book_url failed to generate from {:search=>"Dracula", :controller=>"books", :action=>"show"}, expected: {:controller=>"books", :action=>"show"}, diff: {:search=>"Dracula"}

delete the line of code presentation, and everything will be fine. Also changing it to

<%= link_to book.title, :controller => "books", :action => "show", :search => book.title %>

makes you work.

I created a standalone rails application that demonstrates my problem in isolation http://github.com/jowls/singular_resource_bug

This is mistake? caused by some combination of special resources and quiet routes?

It was on rails 2.3.10

thank

+3
source share
1

- . , :id books_url ( ). :

book_url(book, :search => 'Dracula') # book.id would also work here

. , Rails.

, , books.

:

class BooksController < ActionController::Base

  def show
    # If :search is blank, populate it with :id value
    params[:search] ||= params[:id]

    # your code ...
  end

end

/ book_url/book_path:

# "Dracula" will be passed to your controller as params[:id]
book_path("Dracula")

/ :

map.resources :books

# Explicit named routes take precedence over generated ones
map.book '/books/:search', :controller => 'books', :action => 'show'

, id: REST, , : id, , .

, .

+3

Source: https://habr.com/ru/post/1771442/


All Articles