[英]Rails: current_user.vote_up(@micropost) undefined method
I am currently getting an undefined method for adding <% if current_user.vote_up?(@micropost) == true %>
, I am unsure what is causing the error because I defined it here in the micropost controller. 我目前正在获得一种不确定的方法,用于添加
<% if current_user.vote_up?(@micropost) == true %>
,我不确定是什么原因引起的错误,因为我是在micropost控制器中定义此错误的。 Any suggestions? 有什么建议么?
Micropost Controller 微站控制器
def vote_up
@micropost = Micropost.find(params[:id])
current_user.vote_exclusively_for(@micropost)
current_user.vote_up(@micropost)
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
def unvote
@micropost = Micropost.find(params[:id])
current_user.vote_exclusively_against(@micropost)
current_user.unvote(@thred)
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
Micropost HTML Micropost HTML
<% if current_user.vote_up?(@micropost) == true %>
<div class='<%=micropost.id %>'>
<a href="/microposts/<%=micropost.id %>/vote_up" data-remote='true' class='CounterButton b2'>
<span id="CounterIcon" class="<%=micropost.id%>"></span>
</a>
</div>
<% else %>
<div class='<%=micropost.id %>'>
<a href="/microposts/<%=micropost.id %>/unvote" data-remote='true' class='CounterButton b2'>
<span id="CounterIcon" class="<%=micropost.id%>"></span>
</a>
</div>
<% end %>
Controller methods can't be called directly like you intend to from the view. 不能像您打算从视图中那样直接调用控制器方法。
Furthermore, there seems to be a bit of confusion on how the MVC separation works in Rails. 此外,MVC分离在Rails中的工作方式似乎有些混乱。
Your current_user variable (in the view) corresponds to the "Model" layer, and as such, vote_up?
您的current_user变量(在视图中)对应于“模型”层,因此,
vote_up?
should be a method of whichever class current_user is an instance of. 应该是current_user类的实例的方法。 This would be defined in
models/User.rb
or something like that, and there should be a vote_up?
这将在
models/User.rb
或类似的文件中定义,并且应该有一个vote_up?
method that just queries the user's instance and returns a boolean value. 仅查询用户实例并返回布尔值的方法。
This has nothing to do with the controller's vote_up action. 这与控制器的表决动作无关。 This will not return a value per se: I understand it to actually vote up a "micropost".
这本身不会返回任何值:我理解它实际上是在投票赞成一个“微博”。 A method in the controller will be fired in response to a user's request, do some stuff to objects from the database (model layer), possibly put some of these model objects in variables, and invoke a view to show information to the user (possibly taken from these objects).
控制器中的方法将响应用户的请求而被触发,对数据库(模型层)中的对象做一些填充,可能将其中一些模型对象放入变量中,并调用视图向用户显示信息(可能取自这些对象)。 But methods in the controllers aren't meant to be "called" from the view.
但是,控制器中的方法并不是从视图中“被调用”的。
Incidentally, doing == true
as you do in the Micropost HTML is redundant as by convention, methods ending in ?
顺便说一句,按照惯例,在Micropost HTML中执行
== true
是多余的,按照惯例,方法以?
结尾?
return a boolean value. 返回一个布尔值。 You can just:
您可以:
<% if current_user.vote_up?(@micropost) %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.