2012-12-14 79 views
1

嘗試獲取通過params [:hashtag]傳遞給create.js.erb文件中的AJAX事件的變量。它似乎沒有通過,因爲它顯示空白。它也沒有拋出一個錯誤。rails通過js.erb使用jquery和ajax傳遞param變量

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

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

search.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 %> 

回答

3

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

$(".swag_text_field").val("<%= params[:hashtag] %>"); 
+1

哇,謝謝!像魅力一樣工作,所以params通過,除了format.js之外沒有做任何其他事情 – thebusiness11