我有一个部分我试图通过 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