1

试图获取一个通过 params[:hashtag] 传递到 create.js.erb 文件中的 AJAX 事件的变量。它似乎没有通过,因为它显示为空白。它也不会引发错误。

`$(".swag_text_field").val(<%= escape_javascript(@hashtag_scrubbed) %>)`

创建.js.erb

$(".votes-in").html("<%= escape_javascript(render(:partial => 'shared/vote_tweets', :object => @random_hashtags)) %>")
$(".tag-history").html("<%= escape_javascript(render(:partial => 'shared/tag_history', :object => @vote_history)) %>")
$(".leaderboard-tag-history").html("<%= escape_javascript(render(:partial => 'shared/leaderboard', :object => @leaderboard)) %>")
$(".swag_text_field").val(<%= params[:hashtag] %>)
$(".footer-stats").html("<%= escape_javascript(render(:partial => 'shared/stats')) %>")

hashtag_controler.rb

class HashtagsController < ApplicationController

    def cast_vote                                           #Voting in the middle of the page
    Hashtag.cast_vote(params[:cast_vote])                   #Tells which hashtag to vote for
    @vote_history = Hashlog.vote_history                    #updates the vote history on the bottom of the page
    @leaderboard = Hashtag.leaderboard_history_current      #updates the leaderboard on the right
        @cast_vote_hashtag = Hashtag.cast_vote_hashtag(params[:hashtag])
        respond_to do |format|
        format.html { redirect_to root_path }
        format.js
        end
    end 



    def home    
        @vote_history = Hashlog.vote_history
        @leaderboard = Hashtag.leaderboard_history
        # @trends_pull = Trend.trends_pull
        @trends_display = Trend.trends_display
    end

    def create 
        @stats_total = Hashtag.count 
        @stats_wins = Hashtag.sum(:wins)
        @stats_views = Hashtag.sum(:view_count)
        @stats_losers = (@stats_views - @stats_wins) 
        @vote_history = Hashlog.vote_history
        if signed_in?
            Hashtag.create_hashtag_signed_in(params[:hashtag])
        else
            Hashtag.create_hashtag_guest(params[:hashtag])
        end
        Hashlog.create_hashlog(params[:hashtag])
        @random_hashtag_pull = Hashtag.random_hashtags_pull
        @leaderboard = Hashtag.leaderboard_history_current
        respond_to do |format|
            format.html { redirect_to root_path }
            format.js
        end
    end
end

搜索.html.erb

<%= form_tag hashtags_path, class: "search-form", remote: true do %>
            <div class="input-prepend input-append">
            <span class="add-on swag">#</span>

            <%= text_field_tag :hashtag, nil, onkeypress: "return isNumberKey(event)", class: "span4 swag_text_field", id:"appendedPrependedInput" %>

            <%= submit_tag "VS!", class: "btn add-on-right swag_button" %>
            <% end %>
4

1 回答 1

3

替换$(".swag_text_field").val(<%= params[:hashtag] %>)

$(".swag_text_field").val("<%= params[:hashtag] %>");
于 2012-12-14T07:16:07.703 回答