2012-01-10 3 views
0

私はAjaxクエリーをレンダリングしようとしていますが、どうしたらいいか分かりません。Railsでajaxクエリーをレンダーする

これは形式です:

<%= form_tag(root_path, :method => "post",:id =>"formid", :remote => true) do %> 
    <%= label_tag(:number1, "El que quiero") %> 
    <%= text_field_tag :quiero, nil, :class => 'drug_autocomplete' %> 
    <%= hidden_field_tag :number1 %> 

    <%= label_tag(:number1, "El modelo que tengo") %> 
    <%= text_field_tag :tengo, nil, :class => 'drug_autocomplete' %> 
    <%= hidden_field_tag :number2 %> 

    <%= label_tag(:size1, "Talla de el que tengo") %> 
    <%= text_field_tag :size1%> 

    <%= submit_tag("Do it!",:id =>"submit_but") %> 

<% end %> 

これはかなりコントローラ

def index 
    size1 = params[:size1] 
    number1 = params[:number1] 
    number2 = params[:number2] 
    quiero = params[:quiero] 
    tengo = params[:tengo] 

if (item1 and item2) 

     @itemsok = Contribution.where("first_item_id = ?",item1.id).where("second_item_id = ?",item2.id).where("second_item_grade = ?",size1) 

     end 

である私が送信するためのボタンを押したときに[OK]を、ので、私はデータベースに相談があることがわかります正しい値を書いたときにそれを返し、すべてが完璧に機能します。

どのようにするかわからないのは、クエリのアイテムをレンダリングすることです。 私はAJAXなしでこれをやったことがありますが、このようなものです。

<%if not @itemsok.nil? 
    @itemsok.each do |searches| 
%> 
<table> 
<tr> 
    <td style="width:100px;"><%= searches.first_item.description %> </td> 
    <td style="width:150px; font-size:30px;"><%= searches.first_item_grade %> </td> 


    <td style="width:150px;"><%= searches.second_item.description %> </td> 
    <td style="width:150px; font-size:30px;"><%= searches.second_item_grade %> </td> 
    <td style="width:150px; font-size:18px;"><a href="<%= contribution_path(searches.id) %>">Show</a> </td> 


</tr> 
</table> 

オクラホマので、それは、コントローラで変数を取り、nillでない場合、それはデータをレンダリング私はレンダリングすることができますどのように

(通常のリクエストでウィッヒを、私はページをリロードアクセスすることができます)私のクエリの項目は? ヒントがあれば幸いです。 ありがとうございます。例えば

+0

HTTP [RAILSにおけるAJAX応答のレンダリング方法](の可能性の重複:// stackoverflowの。 com/questions/8797692/how-to-render-the-ajax-response-in-rails) – iwasrobbed

答えて

1

、_search.html.erb

<%if not @itemsok.nil? 
    @itemsok.each do |searches| 
%> 
<table> 
<tr> 
    <td style="width:100px;"><%= searches.first_item.description %> </td> 
    <td style="width:150px; font-size:30px;"><%= searches.first_item_grade %> </td> 


    <td style="width:150px;"><%= searches.second_item.description %> </td> 
    <td style="width:150px; font-size:30px;"><%= searches.second_item_grade %> </td> 
    <td style="width:150px; font-size:18px;"><a href="<%= contribution_path(searches.id) %>">Show</a> </td> 


</tr> 
</table> 

search.js.erb

$('#container').html("<%= escape_javascript(render :partial => 'search/search')%>"); 
+0

ありがとうございました。 –

関連する問題