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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18659447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:39:31  来源:igfitidea点击:

calling a controller method from view Ruby on rails

ruby-on-rails

提问by saran

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 变量

回答by Keith

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 %>

回答by Teemu Leisti

I don't quite understand what you mean when you say that you have to "call this @countvariable" 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?

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

By convention, a controller's showmethod 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:

按照惯例,控制器的show方法显示相关数据库中一行的信息。如果目的是在该视图中显示课程的学生人数,我会在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

<%= @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 showtemplate 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:

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

回答by saran

declare method name as the helper method

将方法名称声明为辅助方法

In the view call the method as

在视图中调用方法为

<% count = course_user_count %>

回答by Miotsu

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