Rio
Rio

Reputation: 14882

Routing Rails 3 with string parameter

I'd like to be able to direct

/samplecontroller/search 

with a :q parameter. How do I do this in rails 3? I currently have

 match 'samplecontroller/:search' => 'samplecontroller#search'

but all it gives in rake:routes

/samplecontroller/:search(.:format)   {:action=>"search", :controller=>"samplecontroller"}

The controller expects

def search
    @search = Post.search(:include => [:comments]) do
      keywords(params[:q])
    end
  end

Thanks!

Upvotes: 0

Views: 219

Answers (2)

Larsenal
Larsenal

Reputation: 51156

In routes.rb:

 match 'samplecontroller/search' => 'samplecontroller#search'

Your version said that :search was a parameter for the route. Removing the leading colon tells it to match that whole string literally.

Upvotes: 0

Dogbert
Dogbert

Reputation: 222188

Just do

match 'samplecontroller/search' => 'samplecontroller#search'

Upvotes: 2

Related Questions