Was*_*asi 3 ruby-on-rails ruby-on-rails-3
我接受用户的意见
在我的索引操作中,我有:
@b = Ece.find_by_code("#{params[:course]}")
Run Code Online (Sandbox Code Playgroud)
我想在show动作和index动作中使用@b中存储的值.
在我的节目动作中,我想做这样的事情:
<p><%= link_to "Forum", href="/courses/#{Course.find_by_courseCode(<%= @b %>).id}", :class=>"question_button round" %>
Run Code Online (Sandbox Code Playgroud)
如何将@b设置为全局,以便这两个操作都可以使用它
注意:Ece和Course是两种不同的模型
Was*_*asi 14
我只是想用一种方法来声明@b是全局的.
使用$ b代替@b就可以了
您可以将它包含在之前的过滤器中,如下所示:
before_filter :find_b, :only => [:index, :show]
# Standard controller actions here.
private
def find_b
@b = Ece.find_by_code("#{params[:course]}")
end
Run Code Online (Sandbox Code Playgroud)
控制器过滤器是ActionController的一部分,您可以在此处找到有关它们的文档.除了在操作之前运行的过滤器之前,还有过滤器之后以及分别在操作周围和之后运行的过滤器之后.
| 归档时间: |
|
| 查看次数: |
12422 次 |
| 最近记录: |