How do I create a routes.rb entry for a controller

2019-09-02 11:40发布

问题:

I'm trying to populate an iframe with some erb and I can't figure out how to create a route to the new controller.

class TestController < ApplicationController
  def iframe
    load_channels
    render :partial => "report", :layout => "test"
  end
end

This is in my webpage:

<iframe src="<%= url_for controller: 'test', action: 'iframe' %>" name="report"></iframe>"

I created a layout with basic html and erb.

I'm getting "no route matches" error. When I look at the routes.rb file it's one of the most confusing config files that I've ever seen.

How do I route to this controller?

回答1:

You can try to put get ‘htmlfilename’, to:’test#iframe’in the routes.rb and see if it works. The htmlfilename should be without the extension. And in the url_for you can put the path rather than controller: 'test', action: 'iframe'. For more information you can check the Rails routing documentation here.