简体   繁体   English

Rails方法在View中不可用

[英]Rails Method not available in View

I get this strange error. 我收到这个奇怪的错误。 I have defined a method for my model 我为我的模型定义了一个方法

    class Rating < ActiveRecord::Base

  [...]

  belongs_to :user
  belongs_to :movie

  [...]

  def to_text
    texto = case self.grade
            when 0..1 then "horrible"
            when 2..3 then "bad"
            when 4..5 then "not bad"
            when 6..7 then "good"
            when 8..9 then "very good"
            when 10 then "master piece"
            end
  end
end

Then, on my controller, I define this instance: 然后,在我的控制器上,我定义了这个实例:

@current_user_rating=@movie.ratings.where(:user_id => current_user)

And it does find it, so it works. 它确实找到了它,所以它有效。 But then, when I call the method, or a property like 但是,当我调用方法或类似的属性时

<%= @current_user_rating.grade %>
<%= @current_user_rating.to_text %>

I get this error 我收到这个错误

undefined method `grade' for []:ActiveRecord::Relation
undefined method `to_text' for []:ActiveRecord::Relation

Why the variable not behaving as an instance with appropriate attributes and methods but as a Relation? 为什么变量不是具有适当属性和方法的实例,而是作为Relation?

It does work on the Console, but not on the server... 它可以在控制台上运行,但不能在服务器上运行......

since a movie has multiple ratings, @current_user_rating is a collection of them, even if there is one. 由于电影有多个评级,@ current_user_rating是它们的集合,即使有一个。 you should be able to get rid of the errors by calling your methods like this: 你应该能够通过调用这样的方法来摆脱错误:

<% @current_user_rating.each do |rating| %>
  <%= rating.grade %>
  <%= rating.to_text %>
<% end %>

When you call where it doesn't actually query the database, it creates an object which can be converted and run on the database. 当你调用where实际上它并不查询数据库,它创建可转换并在数据库上运行的对象。 This is useful for further modifying the query before it is run. 这对于在运行之前进一步修改查询非常有用。

Typically a further call to first or all is added to actually execute the query and get the result. 通常会添加对firstall的进一步调用以实际执行查询并获取结果。 (Note that this might be done automatically if you call where in the rails console, so that the result can be printed) (请注意,如果您在rails控制台中调用where ,则可以自动完成此操作,以便可以打印结果)

In your case, it looks like a user is expected to rate a movie no more than once, so the following seems appropriate: 在您的情况下,看起来用户应该对电影评级不超过一次,因此以下似乎是合适的:

@current_user_rating=@movie.ratings.where(:user_id => current_user).first

EDIT : Actually I think GSto is correct, what I wrote above does not look like the reason for your failure. 编辑 :其实我认为GSto是正确的,我上面写的内容看起来不像你失败的原因。 The error message is actually complaining that the methods you are trying to call aren't valid to be called on an Array of objects. 错误消息实际上是抱怨您尝试调用的方法无法在对象数组上调用。 In this case, first is simply selecting the first result and ignoring any others. 在这种情况下, first只是选择第一个结果并忽略任何其他结果。

It would be good to double-check if you have any movies for which a single user has multiple ratings. 如果您有任何单个用户有多个评级的电影,那么请仔细检查。 In fact, I'd recommend adding a validation to ensure that this is not allowed, if it is not intended. 事实上,我建议添加一个验证,以确保不允许这样做,如果不是这样的话。

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

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