【发布时间】:2015-06-02 03:35:45
【问题描述】:
这里有基本的帮助,但我是 Ruby On Rails 的新手。 Windows 7 上的 Rails 版本 4.1.7。
我希望看到为链接创建的不同 url。代码和问题描述如下。
感谢您的帮助!
-T
控制器:
class DemoController < ApplicationController
layout false
def index
end
def hello
#render('hello')
@array = [1,2,3,4,5]
end
def other_hello
redirect_to(:controller => 'demo', :action => 'index')
end
def lynda
redirect_to("http://lynda.com")
end
end
routes.rb:
Rails.application.routes.draw do
root "demo#index"
#get 'demo/index'
match ':controller(/:action(:id))', :via => :get
end
index.html.erb('/demo/index'):
<h1>Demo#index</h1>
<p>Hello From Index</p>
<a href="/demo/hello">Hello Page 1</a><br />
<%= link_to( "Hello page 2c", {action: "hello" })%> <br />
<%= link_to("Hello with Parameters", {action:"hello", page: 5, id: 20}) %>
rake 路线:输出
前缀动词 URI 模式控制器#Action 根 GET / 演示#index GET /:controller(/:action(:id))(.:format) :controller#:action
来自 index.html 的结果源:
<h1>Demo#index</h1>
<p>Hello From Index</p>
<a href="/demo/hello">Hello Page 1</a><br />
<a href="/demo">Hello page 2c</a> <br />
<a href="/demo/hello20?page=5">Hello with Parameters</a>
问题:
我希望“Hello page 2c”链接的 href 为“/demo/hello”。
我还希望“带参数的 Hello”链接的 href 为“/demo/hello/20?page=5”
不知道进一步看什么。
我尝试了不同的方法来格式化 link_to,但它们都给出了相同的结果。
前:
<%= link_to( "Hello page 2c", {:action => "hello" })%> <br />
<%= link_to("Hello with Parameters", {:action => "hello", page: 5, id: 20}) %><br />
<%= link_to "Hello page 2c", action: "hello" %> <br />
<%= link_to "Hello with Parameters", action: "hello", page: 5, id: 20 %><br />
<%= link_to( "Hello page 2c", {controller: "demo", action: "hello" })%> <br />
<%= link_to("Hello with Parameters", {controller: "demo", action:"hello", page: 5, id: 20}) %><br />
【问题讨论】:
标签: ruby-on-rails link-to