Does not route directly to template files

Asked 2 years ago, Updated 2 years ago, 39 views

According to Rails routing rules, the template should be routed directly under the following conditions:
·Routing is defined in config/routes.rb [get'view/keyword']
·There is no controller that supports the routing above
·The template corresponding to the routing above exists [app/views/view/keyword.html.erb]

However, if you actually access the routes.rb definition URL (http://localhost:3000/keyword/search), you will get Routing Error (uninitialized constant KeywordController)

.

Now you can create a corresponding controller [app/controllers/view_controller.rb] with the following information to successfully display the page.

class ViewController <ApplicationController
  def keyword
  end
end

Therefore, it seems that the rules for routing directly to the template do not work, but if you have any idea of this, could you tell me how to solve this problem?

ruby-on-rails ruby

2022-09-30 19:10

1 Answers

I'm sorry, but it's solved. If there is no action method in the controller instead of the controller, it will route directly to the template.

class ViewController <ApplicationController
  def keyword ← action method (not required)
  end ← action method (not required)
end

The controller itself and its class definition are necessary.Excuse me.


2022-09-30 19:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.