简体   繁体   English

从视图Ruby on rails调用控制器方法

[英]calling a controller method from view Ruby on rails

I am very new to ruby. 我对红宝石很新。 I have one doubt, how to call a controller method from a view. 我有一个疑问,如何从视图中调用控制器方法。

my controller 我的控制器

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

I have to call this @count variable from the method in my view course.view.html.erb 我必须从我的视图course.view.html.erb中的方法中调用这个@count变量

At the top of your controller you can mark the method that you want available to your views as a helper method: 在控制器的顶部,您可以将视图中可用的方法标记为辅助方法:

helper_method :course_user_count

Then in your view, you can call course_user_count and store the result. 然后在您的视图中,您可以调用course_user_count并存储结果。

<% count = course_user_count %>

I don't quite understand what you mean when you say that you have to "call this @count variable" from your view. 当你说你必须从你的视图中“调用这个@count变量”时,我不太明白你的意思。 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.) (你没有“调用”变量,你读它或设置它。)

Second, your method reads each course's student count and then assigns it to the variable @count . 其次,您的方法读取每个课程的学生计数,然后将其分配给变量@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? 我不确定你在这里要做什么 - 或许通过为每门课程预先设定这个值来“初始化”控制器?

By convention, a controller's show method shows the information for one line of the associated database. 按照惯例,控制器的show方法显示相关数据库的一行信息。 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 : 如果目的是在该视图中显示课程的学生数,我会在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

And display the variable's value like this in template app/views/courses/show.html.erb : 并在模板app/views/courses/show.html.erb显示变量的值,如下app/views/courses/show.html.erb

<%= @student_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. 换句话说,我不会在控制器中写一个方法来返回课程的学生数,而只是将它作为参数传递给视图,就像我传递视图需要显示的任何其他内容一样 - 或者至少通过一个非常简单的操作无法访问视图的任何内容, @course.students.count没有真正满足的条件,但这是一个品味问题。

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. 但是,在控制器中定义一个方法可能有意义,因为每次show模板时,计算更复杂和/或不需要这些值。 To make that method callable from your views, the method has to be declared a helper method, as Keith mentioned in his answer. 为了使该方法可以从您的视图中调用,该方法必须被声明为辅助方法,正如Keith在他的回答中提到的那样。 For instance, a method that returns the total student count of all courses in app/controllers/course_controller.rb : 例如,一个返回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

Use the method like this in any template under app/views/courses/ to display the value returned by that method: app/views/courses/下的任何模板中使用这样的方法来显示该方法返回的值:

<%= total_student_count %>

declare method name as the helper method 声明方法名称作为辅助方法

In the view call the method as 在视图中调用方法为

<% count = course_user_count %>

Your controller is accessible in your view through the variable: 您可以通过变量在视图中访问您的控制器:

@controller

which is available by default. 这是默认可用的。 I'm talking about the controller associated with that particular view of course. 我当然是在谈论与特定视图相关的控制器。

In your case: 在你的情况下:

@controller.course_user_count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM