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

ハッシュタグコントローラー

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

create.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

はい、 @random_hashtags に値を与えることと同じではありません。マニュアルを読まずに、部分的な "_vote_tveet" で:object => @random_hashtags変数を使用できると言っていると思います(その場合、開始されなければならない値があります) . したがって、まだパーシャルで使用していて、コントローラーで値を指定している場合は、create.js.erb の行をスキップする必要があります。vote_tweet@random_hashtags@random_hashtags:object => @random_hashtags

于 2012-12-03T00:23:29.613 に答える
0

@random_hashtags = Hashtag.order("RANDOM()").limit(4) を定義すると役立ちます

作成コントローラーで。

于 2012-12-02T22:42:07.217 に答える