Rails:current_user.vote_up(@micropost)未定义方法
我目前得到一个未定义的方法来添加 <% if current_user.vote_up?(@micropost) == true %>
,我不确定是什么导致了错误,因为我在此处定义了它微帖子控制器。有什么建议吗?
微帖子控制器
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
微帖子 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 %>
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. 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
<% 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 %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
控制器方法不能像您打算从视图中那样直接调用。
此外,对于 Rails 中的 MVC 分离如何工作似乎有些混乱。
您的 current_user 变量(在视图中)对应于“模型”层,因此,
vote_up?
应该是 current_user 所属类的实例的方法。这将在models/User.rb
或类似的内容中定义,并且应该有一个vote_up?
方法,该方法仅查询用户的实例并返回布尔值。这与控制器的 vote_up 操作无关。这本身不会返回值:我理解它实际上是对“微帖子”进行投票。控制器中的方法将响应用户的请求而被触发,对数据库(模型层)中的对象执行一些操作,可能将其中一些模型对象放入变量中,并调用视图向用户显示信息(可能是从这些对象中获取)。但是控制器中的方法并不意味着从视图中“调用”。
顺便说一句,像在 Micropost HTML 中那样执行
== true
是多余的,因为按照惯例,以?
结尾的方法会返回一个布尔值。你可以: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.
Your current_user variable (in the view) corresponds to the "Model" layer, and as such,
vote_up?
should be a method of whichever class current_user is an instance of. This would be defined inmodels/User.rb
or something like that, and there should be avote_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?
return a boolean value. You can just: