Routes.rb structure


(SMHassanAlavi) #1

hi
I have a controller and action but my action don’t render any thing .it just send some json to some where.
my routs.rb :

get 'recom/recom'
resources :recom do
      member do
          get 'recom/req'
      end
  end

and my recom controller:

def index
         render('recom/recom')
   end
    def req
        require 'net/http'
        require 'json'
        uri = URI("http://80.85.203.33:321/ingest?id=1122&url=%27t54646%27");
        req = Net::HTTP::Get.new(uri);
        res = Net::HTTP.start(uri.hostname, uri.port) do |http|
            http.request(req);
        end
    end

and my error is:
The action ‘show’ could not be found for RecomController
and when I put show function without render it says it couldn’t run blank template.
when I put render inside of req function .It works fine but I don’t want to render any thing I just want to run a code on every request sent to controller


(James Cook) #2

It sounds like you probably don’t want to be using controller actions then. Controllers are typically middlemen between models and views. Hence they’re expecting you to render something, whether that’s html, json or just plain headers.

Have you fully read up on Routes and Controllers?

Also, is there a particular reason why you’re defining recom as a resource and then using a member route? This will attempt to use the recom controller and whatever action is after get, in this case ‘recom/req’ which I’m not sure is valid but I may be wrong. It be might be trying to default to the show action hence your error.

If you just wanted to provide a path just for the req action you could just use

get 'recom/req', to: 'recom#req'

Anyway, what relevance does this have to Discourse? :thinking: