从视图 Ruby on rails 调用控制器方法 [英] calling a controller method from view Ruby on rails

查看:29
本文介绍了从视图 Ruby on rails 调用控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 ruby​​ 很陌生.我有一个疑问,如何从视图调用控制器方法.

I am very new to ruby. I have one doubt, how to call a controller method from a view.

我的控制器

def course_user_count
 @courses=Course.all
 @courses.each do |course|
 @count=course.students.count
end

我必须从我的视图 course.view.html.erb 中的方法中调用这个 @count 变量

I have to call this @count variable from the method in my view course.view.html.erb

推荐答案

我不太明白你说从你的角度来看你必须调用这个 @count 变量"是什么意思.您没有在控制器中设置该变量吗?如果是这样,它应该可以在关联的视图中自动访问.(您不是调用"变量,而是读取或设置它.)

I don't quite understand what you mean when you say that you have to "call this @count variable" from your view. Are you not setting that variable in your controller? If so, it should automatically be accessible in the associated view. (You don't "call" a variable, you read it or set it.)

其次,您的方法读取每个课程的学生人数,然后将其分配给变量 @count.每次写入此变量时,其先前的值都会被覆盖,因此写入的方法是无用的.我不确定您要在这里做什么——也许通过为每门课程提前设置这个值来初始化"控制器?

Second, your method reads each course's student count and then assigns it to the variable @count. Each time this variable is written to, its previous value is overwritten, so the method as written is useless. I'm not sure what you're trying to do here -- perhaps "initializing" the controller by setting this value in advance for each course?

按照惯例,控制器的 show 方法显示相关数据库的一行的信息.如果目的是在该视图中显示课程的学生人数,我会在 app/controllers/course_controller.rb 中编写如下内容:

By convention, a controller's show method shows the information for one line of the associated database. If the aim is to show the course's student count in that view, I would write something like this in app/controllers/course_controller.rb:

class CourseController < ApplicationController

  def show
    @course = Course.find(params[:id]) # Here I assume that the url is .../courses/<id>
    @student_count = @course.students.count
  end

  ...

end

并在模板中像这样显示变量的值app/views/courses/show.html.erb:

And display the variable's value like this in template app/views/courses/show.html.erb:

<%= @student_count %>

换句话说,我不会在控制器中编写一个方法来返回课程的学生人数,而是将其作为参数传递给视图,就像传递视图需要显示的任何其他内容一样——或者至少视图无法通过非常简单的操作访问的任何内容,@course.students.count 并未真正满足该条件,但这是一个品味问题.

In other words, I wouldn't write a method in the controller to return a course's student count, but instead just pass it as a parameter to the view, just as I would pass anything else the view needs to display -- or at least anything that the view can't access by a very simple operation, a condition not really fulfilled by @course.students.count, but that's a matter of taste.

但是,对于计算更复杂和/或每次显示 show 模板时不需要的值,在控制器中定义一个方法可能是有意义的.要使该方法可从您的视图中调用,必须将该方法声明为辅助方法,正如 Keith 在他的回答中提到的那样.例如,返回 app/controllers/course_controller.rb 中所有课程的学生总数的方法:

However, it might make sense to define a method in the controller for values that are more complex to compute and/or are not needed every time the show template is displayed. To make that method callable from your views, the method has to be declared a helper method, as Keith mentioned in his answer. For instance, a method that returns the total student count of all courses in app/controllers/course_controller.rb:

class CourseController < ApplicationController

  helper_method :total_student_count

  def total_student_count
    total_count = 0
    Course.all.each do |c|
      total_count += c.students.count
    end
    total_count
  end

  ...

end

app/views/courses/下的任何模板中使用这样的方法来显示该方法返回的值:

Use the method like this in any template under app/views/courses/ to display the value returned by that method:

<%= total_student_count %>

这篇关于从视图 Ruby on rails 调用控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆