Search code examples
ruby-on-railsparameterscontrollerfriendly-id

Rails: How to pass params from a friendly_id url to a controller?


I have an URL :

Example.com/searchjobs/jobs/sales

So, I just want to pass 'sales' keyword to my @query variable in my controller, but it remains nil.

I tried several tutorials but they only show how to use friendly_id with a model.

Thanks for your help.

searchjobs_controller.rb

class SearchjobsController < ApplicationController

def jobs
    
    require 'nokogiri'
    require 'open-uri'
    @query = params[:qw]
 
  

   
    @xmls = Nokogiri::XML(open(URI.escape("http://api.indeed.com/ads/apisearch?publisher=apikey&q=#{@query}&l=&sort=date&radius=&st=&jt=&start=&limit=50&fromage=&filter=&co=uk&userip=#{@ip}&useragent=#{@useragent}&v=2")))
    
end
end

Model searchjob.rb

class Searchjob < ActiveRecord::Base
  extend FriendlyId
  friendly_id :qw
 end
end

Routes.rb

match '*qw',    to: 'searchjobs#jobs', via: :get


Solution

  • Try to change your route to this:

    match '/searchjobs/jobs/:qw', to: 'searchjobs#jobs', via: :get