This question already has an answer here:
这个问题在这里已有答案:
- Best way to highlight current page in Rails 3? — apply a css class to links conditionally 8 answers
- 在Rails 3中突出显示当前页面的最佳方法? - 应用css类有条件地链接8个答案
I am new to Rails. In my appliction.html.erb, i am using a render partial: to use a html_css shared file. This shared file contains buttons that can be used by different pages. For example, for the pages /posts and /answers a search field with a suubmit button taken from the shared filed is displayed. I would like to make this button work for both pages. When we are in the /posts page, we search in the posts and when we are in the /answers page, we search in the answers.
我是Rails的新手。在我的appliction.html.erb中,我使用了渲染部分:使用html_css共享文件。此共享文件包含可供不同页面使用的按钮。例如,对于页面/帖子和/答案,显示具有从共享字段中取出的提交按钮的搜索字段。我想让这个按钮适用于两个页面。当我们在/ posts页面中,我们搜索帖子,当我们在/ answers页面时,我们搜索答案。
So, i need to call the right controller each time. For that, i need a helper or a trick that enables me to know in which page i am (which controller#index did i call?).
所以,我每次都需要调用正确的控制器。为此,我需要一个助手或技巧,让我知道我在哪个页面(我打电话给哪个控制器#index?)。
Does rails contain a method or a helper enabling this?
rails是否包含启用此方法的方法或帮助程序?
thanks :)
谢谢 :)
2 个解决方案
#1
0
Use the helper functions: controller_name
and action_name
.
使用辅助函数:controller_name和action_name。
If you have a controller:
如果你有一个控制器:
class UsersController < ApplicationController
def my_action
# stuff
end
end
In the view, you can do:
在视图中,您可以执行以下操作:
Controller name: <%= controller_name %> <br/> # => users
Action name: <%= action_name %> <br/> # => my_action
See: http://guides.rubyonrails.org/action_controller_overview.html#routing-parameters
请参阅:http://guides.rubyonrails.org/action_controller_overview.html#routing-parameters
#2
0
Action and controller are in the params already. I imagine you would rather do stuff with that information rather than just display it with controller_name/action_name
动作和控制器已经在params中。我想你宁愿用这些信息来做事情,而不是只用controller_name / action_name来显示它
for instance to display a footer only in the about page assuming the controller is called about_controller you could
例如,假设控制器被称为about_controller你可以在about页面中显示一个页脚
<% if params[:controller] == about %>
<%= render partial: 'footer' %>
<% end %>
params[:action]
will contain the action.
params [:action]将包含该动作。
#1
0
Use the helper functions: controller_name
and action_name
.
使用辅助函数:controller_name和action_name。
If you have a controller:
如果你有一个控制器:
class UsersController < ApplicationController
def my_action
# stuff
end
end
In the view, you can do:
在视图中,您可以执行以下操作:
Controller name: <%= controller_name %> <br/> # => users
Action name: <%= action_name %> <br/> # => my_action
See: http://guides.rubyonrails.org/action_controller_overview.html#routing-parameters
请参阅:http://guides.rubyonrails.org/action_controller_overview.html#routing-parameters
#2
0
Action and controller are in the params already. I imagine you would rather do stuff with that information rather than just display it with controller_name/action_name
动作和控制器已经在params中。我想你宁愿用这些信息来做事情,而不是只用controller_name / action_name来显示它
for instance to display a footer only in the about page assuming the controller is called about_controller you could
例如,假设控制器被称为about_controller你可以在about页面中显示一个页脚
<% if params[:controller] == about %>
<%= render partial: 'footer' %>
<% end %>
params[:action]
will contain the action.
params [:action]将包含该动作。