Ruby-on-rails 没有路由匹配 [GET] "/"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12880951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:51:59  来源:igfitidea点击:

No route matches [GET] "/"

ruby-on-railsrubyruby-on-rails-3ruby-on-rails-3.2

提问by iJade

I'm new to rails so it may sound quite naive.I'm getting this error

我是 Rails 的新手,所以听起来可能很天真。我收到了这个错误

No route matches [GET] "/"

Here is my routes.rb

这是我的routes.rb

MyApp::Application.routes.draw do
    match 'welcome/contact' => 'welcome#index'
end

Here is my controller

这是我的控制器

class WelcomeController < ApplicationController
   def index
      redirect_to :action => :contact
   end
   def contact

   end
end

And i have a contact.html.erb in my app/view/welcome/.What am i doing wrong?

我的 app/view/welcome/ 中有一个 contact.html.erb。我做错了什么?

回答by Jean

I don't understand what you want to do. But I think you want your view Welcome/contact as your index page, if this is correct, you only have to change your routes.rb file like this:

我不明白你想做什么。但我认为您希望您的“欢迎/联系人”视图作为您的索引页面,如果这是正确的,您只需像这样更改您的 routes.rb 文件:

root to: 'welcome#contact'

and you have to remove the index.html file from the public folder.

并且您必须从公共文件夹中删除 index.html 文件。

On the other hand, you can read more of rails routes here

另一方面,您可以在此处阅读更多 rails 路线

回答by kanna

You need to create a route for the actions other than CRUD actions in controller.This will solve the issue for all actions.

您需要为控制器中除 CRUD 操作之外的操作创建路由。这将解决所有操作的问题。

match ':controller(/:action)'

回答by rewritten

What you want to do is to RENDER the contact page, not to redirect to another controller and action.

你想要做的是渲染联系页面,而不是重定向到另一个控制器和动作。

Just put the code in the contact view into the app/views/welcome/index.html.erbfile, and live happily.

只需将联系人视图中的代码放入app/views/welcome/index.html.erb文件中,就可以愉快地生活。

回答by Chen Fisher

You need to add a contactaction to your WelcomeController

您需要向WelcomeController添加联系操作

class WelcomeController < ApplicationController
  def index
     redirect_to :action => :contact
  end

  def contact
  end
end