Yos*_*sef 7 ruby ruby-on-rails parameter-passing rails-routing ruby-on-rails-3
我想将一个参数传递给索引操作,但我只是得到了show动作.
routes.rb中:
Test1::Application.routes.draw do
resources :blog
end
Run Code Online (Sandbox Code Playgroud)
blog_controller.rb:
def show
# code
end
def index
# code
end
Run Code Online (Sandbox Code Playgroud)
查看发送以显示操作的网址而不是索引操作:
<a href="/blog/myvar"> My link </a>
Run Code Online (Sandbox Code Playgroud)
我应该在路线文件或视图中添加什么?
输出我的路线:
$ rake routes
blog GET /blog(.:format) {:action=>"index", :controller=>"blog"}
blog GET /blog/:id(.:format) {:action=>"show", :controller=>"blog"}
Run Code Online (Sandbox Code Playgroud)
命令行将显示您可以使用的路由 rake routes
您想要的路线是blogs_path,您可以在其上添加参数,例如blogs_path(other_item => :value).
具体取决于您是否尝试在控制器,另一个视图等中使用它.
对于视图有: <%= link_to 'My Link', blogs_path(:other_item => value) %>
| 归档时间: |
|
| 查看次数: |
12468 次 |
| 最近记录: |