ActionView :: Template :: Error(未定义的方法`voted_for?'用于#<user:0x007f6deb7f5368>)</user:0x007f6deb7f5368>

时间:2015-03-27 04:59:24

标签: ruby ruby-on-rails-4 heroku

将rails项目部署到Heroku时出现此错误。这是我的polls/_vote_option.html.erb文件:

<div class="form-group">
    <%= content_tag(:label) do %>
        <% unless current_user.voted_for?(@poll) %>
            <%= radio_button_tag 'vote_option[id]', option.id %>
        <% end %>
        <%= option.title %>
    <% end %>
    <%= visualize_votes_for option %>
</div>

这里是polls/_voting_form.html.erb部分

<%= form_tag votes_path, method: :post, remote: true, id: 'voting_form' do %>
    <%= hidden_field_tag 'poll[id]', @poll.id %>

    <%= render partial: 'polls/vote_option', collection: @poll.vote_options, as: :option %>

    <p><b>Total votes: <%= @poll.vote_summary %></b></p>

    <% if current_user.voted_for?(@poll) %>
        <p>You have already voted!</p>
    <% else %>
        <%= submit_tag 'Vote', class: 'btn btn-lg btn-primary' %>
    <% end %>
<% end %>

这里是user.rb模型(voted_for?方法位于此处):

class User < ActiveRecord::Base

    has_many :votes, dependent: :destroy
    has_many :vote_options, through: :votes
    class << self
        def from_omniauth(auth)
            uid = auth.uid
            info = auth.info.symbolize_keys!
            user = User.find_or_initialize_by(uid: uid)
            user.name = info.name
            user.image_url = info.image
            user.save!
            user
        end

        def voted_for?(poll)
            vote_options(true).any? {|v| v.poll == poll }
        end
    end
end

这是application_controller.rb方法所属的current_user文件:

class ApplicationController < ActionController::Base
# encoding: utf-8
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  def current_user
    @current_user ||= User.find_by(id: cookies[:user_id]) if cookies[:user_id]
  end

  helper_method :current_user
end

完整错误的日志在这里:

2015-03-27T04:36:35.317730+00:00 app[web.1]: Started GET "/polls/1" for 171.232.9.119 at 2015-03-27 04:36:35 +0000
2015-03-27T04:36:35.319482+00:00 app[web.1]:   Parameters: {"id"=>"1"}
2015-03-27T04:36:35.319468+00:00 app[web.1]: Processing by PollsController#show as HTML
2015-03-27T04:36:35.326980+00:00 app[web.1]:   Rendered polls/_vote_option.html.erb (2.4ms)
2015-03-27T04:36:35.327021+00:00 app[web.1]:   Rendered polls/_voting_form.html.erb (3.2ms)
2015-03-27T04:36:35.327088+00:00 app[web.1]:   Rendered polls/show.html.erb within layouts/application (3.4ms)
2015-03-27T04:36:35.329114+00:00 app[web.1]: ActionView::Template::Error (undefined method `voted_for?' for #<User:0x007f8c17ac4220>):
2015-03-27T04:36:35.327184+00:00 app[web.1]: Completed 500 Internal Server Error in 8ms
2015-03-27T04:36:35.329111+00:00 app[web.1]: 
2015-03-27T04:36:35.329122+00:00 app[web.1]:     5:         <% end %>
2015-03-27T04:36:35.329116+00:00 app[web.1]:     1: <div class="form-group">
2015-03-27T04:36:35.329117+00:00 app[web.1]:     2:     <%= content_tag(:label) do %>
2015-03-27T04:36:35.329119+00:00 app[web.1]:     3:         <% unless current_user.voted_for?(@poll) %>
2015-03-27T04:36:35.329125+00:00 app[web.1]:   app/views/polls/_vote_option.html.erb:3:in `block in _app_views_polls__vote_option_html_erb___1797526152723756394_70119836293400'
2015-03-27T04:36:35.329120+00:00 app[web.1]:     4:             <%= radio_button_tag 'vote_option[id]', option.id %>
2015-03-27T04:36:35.329127+00:00 app[web.1]:   app/views/polls/_vote_option.html.erb:2:in `_app_views_polls__vote_option_html_erb___1797526152723756394_70119836293400'
2015-03-27T04:36:35.329133+00:00 app[web.1]: 
2015-03-27T04:36:35.329124+00:00 app[web.1]:     6:         <%= option.title %>
2015-03-27T04:36:35.329128+00:00 app[web.1]:   app/views/polls/_voting_form.html.erb:4:in `block in _app_views_polls__voting_form_html_erb___1028737712551643635_70119836347660'
2015-03-27T04:36:35.329130+00:00 app[web.1]:   app/views/polls/_voting_form.html.erb:1:in `_app_views_polls__voting_form_html_erb___1028737712551643635_70119836347660'
2015-03-27T04:36:35.329131+00:00 app[web.1]:   app/views/polls/show.html.erb:5:in `_app_views_polls_show_html_erb__3711202749271403949_70119836438440'
2015-03-27T04:36:35.329134+00:00 app[web.1]: 

我搜索谷歌找到解决方案,但其他解决方案与我得到的完全不同。我还运行heroku run rake db:migrateheroku restart,但之后没有任何变化。我真的很困惑为什么没有定义voted_for?方法(根据错误的消息)。那么有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

您的方法voted_for?位于class << self内,实际上创建了类方法。在视图中,您正在调用voted_for?,如下所示:current_user.voted_for?(@poll)。其中current_userUser类的对象。因此错误。

您需要的是将该方法从class << self定义中删除:

class << self
    def from_omniauth(auth)
        uid = auth.uid
        info = auth.info.symbolize_keys!
        user = User.find_or_initialize_by(uid: uid)
        user.name = info.name
        user.image_url = info.image
        user.save!
        user
    end

end
def voted_for?(poll)
    vote_options(true).any? {|v| v.poll == poll }
end