简体   繁体   English

在Ruby on Rails中将一页移到另一页?

[英]Move one page to another in Ruby on rails?

In my rails project there is a controller and view named "Welcome/index" where index is an action and another one named "home/page" . 在我的rails项目中,有一个控制器和一个名为“ Welcome / index”的视图,其中index是一个动作,另一个名为“ home / page”。 As i set root"home#page" as my root page. 当我将根“ home#page”设置为我的根页面时。 Now i want to transfer from "page.html.erb" into "index.html.erb" . 现在我想从“ page.html.erb”转移到“ index.html.erb”。 How can i do that. 我怎样才能做到这一点。 And the code i written is below.Do i have to enter some thing in my controller class. 我写的代码在下面。我必须在控制器类中输入一些东西吗? please suggest. 请提出建议。 these are the links that i tried. 这些是我尝试过的链接。 ( How to create an anchor and redirect to this specific anchor in Ruby on Rails ) 如何在Ruby on Rails中创建锚并重定向到该特定锚

<a rel="nofollow" href="index.html.erb">Transfer to index</a>  

You are not supposed to link to .html.erb files, you should link to the methods (not exactly the name of the method, but the name of the route) of a controller. 您不应该链接到.html.erb文件,而应该链接到控制器的方法(不完全是方法的名称,而是路由的名称)。

I strongly encourage you to review the ruby on rails MVC principles. 我强烈建议您回顾Rails MVC原理上的红宝石。 You can read about routing and linking aswell. 您还可以阅读有关路由和链接的信息

Responding to your question, check out the command "rake routes". 在回答您的问题时,请检查命令“ rake route”。 It will list the defined routes of your app and helps you to use them. 它将列出您的应用程序定义的路由,并帮助您使用它们。

Try to replace your code by this: 尝试以此替换您的代码:

<%= link_to 'welcome', welcome_path %>

If you want go for index action of Welcome controller then you can use: 如果要对Welcome控制器执行索引操作,则可以使用:

<%= link_to "Transfer to index", welcome_path %>

Check the rake routes for path. 检查耙路径的路径。

Plese refer link 请参考链接

You need to make sure a named route is defined for welcome/index and then can use the Rails helper link_to to automatically build your link for you in the view. 您需要确保为welcome/index定义了命名路由,然后可以使用Rails帮助器link_to在视图中自动为您构建链接。

In routes.rb : routes.rb

match '/welcome' => 'welcome#index', :as => :welcome

In your page.html.erb view: 在您的page.html.erb视图中:

<%= link_to 'Go to Welcome Page', welcome_path %>
<%= link_to "Link", controller:"controllername" %>

是您应该使用的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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