简体   繁体   English

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

[英]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.

相关问题 (注销时,但未登录时为nil:NilClass的未定义方法`vote_exclusively_for&#39;)-Rails 3(thumbs_up gem) - (undefined method `vote_exclusively_for' for nil:NilClass) when user is logged out, but not when logged in - Rails 3 (thumbs_up gem) Ruby on Rails中nil的未定义方法`save&#39;:@ micropost.save的NilClass - Undefined method `save' for nil:NilClass for @micropost.save in Ruby on Rails Rails路由错误(未定义的局部变量或方法“ micropost_comment”) - Rails Routing Error (undefined local variable or method `micropost_comment') Rails教程第2章错误noMethodError:未定义的方法&#39;micropost&#39; - rails tutorial ch 2 error noMethodError: undefined method 'micropost' 未定义的方法`post_up_vote_path&#39; - undefined method `post_up_vote_path' Ruby on Rails:未定义的方法`current_user?` - Ruby on Rails: undefined method `current_user?` Rails 4:会话-未定义的方法“ current_user” - Rails 4: Sessions - undefined method `current_user' Authlogic和Rails 4-“ current_user是未定义的方法” - Authlogic and Rails 4 - “current_user is undefined method” / posts / 7 / up-vote的未定义方法&#39;stringify_keys&#39;:String - undefined method `stringify_keys' for “/posts/7/up-vote”:String 未定义的方法`vote_for_song_songs_path&#39;[rails 4] - undefined method `vote_for_song_songs_path' [rails 4]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM