简体   繁体   中英

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 

尝试将您的路线更改为此:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM