Rails:current_user.vote_up(@micropost)未定义方法

发布于 2025-01-08 13:27:01 字数 1329 浏览 5 评论 0原文

我目前得到一个未定义的方法来添加 <% 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我心暖 2025-01-15 13:27:01

控制器方法不能像您打算从视图中那样直接调用。

此外,对于 Rails 中的 MVC 分离如何工作似乎有些混乱。

您的 current_user 变量(在视图中)对应于“模型”层,因此,vote_up? 应该是 current_user 所属类的实例的方法。这将在 models/User.rb 或类似的内容中定义,并且应该有一个 vote_up? 方法,该方法仅查询用户的实例并返回布尔值。

这与控制器的 vote_up 操作无关。这本身不会返回值:我理解它实际上是对“微帖子”进行投票。控制器中的方法将响应用户的请求而被触发,对数据库(模型层)中的对象执行一些操作,可能将其中一些模型对象放入变量中,并调用视图向用户显示信息(可能是从这些对象中获取)。但是控制器中的方法并不意味着从视图中“调用”。

顺便说一句,像在 Micropost HTML 中那样执行 == true 是多余的,因为按照惯例,以 ? 结尾的方法会返回一个布尔值。你可以:

<% if current_user.vote_up?(@micropost) %>

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 in models/User.rb or something like that, and there should be a 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 ? return a boolean value. You can just:

<% if current_user.vote_up?(@micropost) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文