0

我有一个部分我试图通过 create.js.erb 加载,问题是当我可以从视图中加载部分时它加载得很好,但是当我尝试在 ajax 操作中调用它时,每个 https都会得到一个 NoMethodError ://gist.github.com/4191176

标签.rb

class Hashtag < ActiveRecord::Base

  attr_accessible :text, :profile_image_url, :from_user, :created_at, :tweet_id, :hashtag, :from_user_name

 def self.pull_hashtag(hashtag)
  hashtag_scrubbed = hashtag
  Twitter.search("%#{hashtag}", :lang => "en", :count => 100, :result_type => "mixed").results.map do |tweet|
    unless exists?(tweet_id: tweet.id)
        create!(
            tweet_id: tweet.id,
            text: tweet.text,
            profile_image_url: tweet.user.profile_image_url,
            from_user: tweet.from_user,
        from_user_name: tweet.user.name, 
            created_at: tweet.created_at,
        hashtag: hashtag
        )   
        end     
    end
  end
end

hashtags_controller

class HashtagsController < ApplicationController

    def home 

    end

    def vote
        @random_hashtags = Hashtag.order("RANDOM()").limit(4)
    end

    def show


    end

    def index

    end

    def create
        Hashtag.pull_hashtag(params[:hashtag])
        respond_to do |format|
        format.html { redirect_to vote_path }
        format.js
    end
     end
end

创建.js.erb

$(".live-votes").slideDown(2222).prepend("<%= escape_javascript(render(:partial => 'shared/vote_tweets', :object => @random_hashtags)) %>")

_vote_tweet 部分

<% @random_hashtags.each do |hashtag| %>
<div class="span4 twitter-spans-v1">
        <div id="tweet-block-v1" class="hashtag-tweet-database-container">
        <div class="tweet-block-border-v1">
        <div class="tweet-block-spacing-v1">
            <div class="twitter-block-author-v1">
            <a class="twitter-block-user-v1" target="_blank" href="https://twitter.com/<%= hashtag.from_user %>">
            <span class="twitter-author-image-v1"><img alt="" class="twitter-author-image-photo-v1" src="<%= hashtag.profile_image_url %>"></span>
            <span class="twitter-author-name-v1"><%= hashtag.from_user_name %></span>
            <span class="twitter-author-nickname-v1">@<%= hashtag.from_user %></span>
            </a>
            <iframe class="twitter-follow-button-v1" scrolling="no" frameborder="0" src="//platform.twitter.com/widgets/follow_button.html#align=right&button=grey&screen_name=<%= hashtag.from_user %>&show_count=false&show_screen_name=false&lang=en" allowtransparency="true">
            </iframe>
            </div>
        <div class="twitter-text-container-v1">
        <p class="twitter-text-field-v1">
        <%= hashtag.text %>
        </p>
        </div>
        <div class="twitter-footer-v1">
            <a class="twitter-view-details-v1" target="_blank"  href="https://twitter.com/<%= hashtag.from_user %>/statuses/<%= hashtag.tweet_id %>">
            <span class="tweet-date-v1"><%= hashtag.created_at.strftime("%d %b %Y") %></span>
            </a>
            <span class="twitter-vote-button-v1"><a href="#" class="btn btn-mini btn-primary">#WINNING</a>
</span>
            <script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
            <ul class="twitter-intent-ul-v1">
                <li class="twitter-intent-li-v1"><a href="https://twitter.com/intent/tweet?in_reply_to=<%= hashtag.tweet_id %>" class="twitter-intent-tweet" title="Reply"></a></li>
                <li class="twitter-intent-li-v1"><a href="https://twitter.com/intent/retweet?tweet_id=<%= hashtag.tweet_id %>"  class="twitter-intent-retweet" title="Retweet"></a></li>
                <li class="twitter-intent-li-v1"><a href="https://twitter.com/intent/favorite?tweet_id=<%= hashtag.tweet_id %>"  class="twitter-intent-favorite" title="Favorite"></a></li>
            </ul>
        </div>
        </div>
        </div>
        </div>
    </div>
                    <% end %>

错误

NoMethodError in Hashtags#create

Showing ./app/views/shared/_vote_tweets.html.erb where line #1 raised:

undefined method `each' for nil:NilClass
Extracted source (around line #1):

1: <% @random_hashtags.each do |hashtag| %>
2: <div class="span4 twitter-spans-v1">
3:      <div id="tweet-block-v1" class="hashtag-tweet-database-container">
4:      <div class="tweet-block-border-v1">
Trace of template inclusion: app/views/hashtags/create.js.erb
4

2 回答 2

1

:object => @random_hashtags的,与给@random_hashtags 一个值不同,没有阅读手册我认为这是说你可以vote_tweet在部分“_vote_tveet”中使用一个变量(在这种情况下,@random_hashtags它具有必须启动的值) . 所以如果你仍然@random_hashtags在你的部分中使用并在控制器中给它一个值,那么你应该跳过:object => @random_hashtagscreate.js.erb 中的行

于 2012-12-03T00:23:29.613 回答
0

如果我定义 @random_hashtags = Hashtag.order("RANDOM()").limit(4) 会有所帮助

在创建控制器中。

于 2012-12-02T22:42:07.217 回答