开发者

Parameter in AJAX request

I have a view which contain multiple links:

<% a.each do |q| %> 
   <%= link_to "stock it", 
        { :action => "stock", 
          :qid => q.question_id, 
          :qur => q.question_answers_url, 
          :qti => q.title } , 
        :remote => true %> 

   <div id="<%= "stock" + q.question_id.to_s %>"></div>
<% end %>

Each link generate AJAX-request. Here is a controller:

def stock
  if(!Later.where(:question_id => params[:qid]).exists?)
    later = Later.new(:question_id => params[:qid], :name => params[:qti], :url => params[:qur])
    later.save
  end 

  respond_to do |format|
    format.js { render :layout=>false }
  end
end  

Now return to the view. Each link has a 'div' with unique id='stock'. When user press the link I need to add text to specific div with correspo开发者_高级运维nding id.

I have a stock.js.erb file:

$("#stock<number>").html("some text");

How can I pass div-id to stock.js.erb and how can I use it ?


Common use is to add object.id to your DOM id. That what you exactly did:

<div id="<%= "stock_#{q.question_id}" %>"></div>

Then in your controller you shoud define your question_id or your exact question:

def stock
  if(!Later.where(:question_id => params[:qid]).exists?)
    later = Later.new(:question_id => params[:qid], :name => params[:qti], :url => params[:qur])
    later.save
  end
  @question_id = params[:qid]
end

Now it will be shared with your stock.js.erb file:

$("#stock_<%= @question_id %>").html("some text");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜