2

ここにフォームがあり、orders/_form.html.erb送信してカテゴリというテーブルに保存します

<%= simple_form_for(@category) do |f| %>

  <%= f.error_notification %>

  <div class="inputs">

    <%= f.input :name, 

:label => false, 

:input_html => { :class => 'text', 

:style => 'float:left; width:250px' }, 

:placeholder => "Type of item / name of expense",


%>



   <%= f.hidden_field :user_id, 

:value => current_user.id %>


   <%= f.button :submit, 

:value => 'Add Category', 

:class => 'small grey',

:style => 'margin:5px 0 0 10px' %>


  </div>



<% end %>

私のcategories_controller.rbには、カテゴリを作成するための次のコードがあります:

# POST /categories

  # POST /categories.json

  def create

    @category = current_user.categories.build(params[:category])



    respond_to do |format|

      if @category.save

        format.html { redirect_to new_order_path }

        format.json { render json: new_order_path, status: :created, location: @category }

      else

        format.html { render action: "new" }

        format.json { render json: @category.errors, status: :unprocessable_entity }

      end

    end

  end

まず、フォームを送信したときにページが更新されないようにするにはどうすればよいですか? 次に、Categories ドロップダウンhttp://d.pr/gUwzにも新しいエントリを追加します (JQ を使用すると想定しています)。これを始める方法はありますか?

4

3 に答える 3

2

Rails 3.x では:remote => trueフォームを使用する必要があります

元:

<%= form_for @comment, :remote => true, :html => { :'data-type' => 'html', :id => 
 <form code>
<% end %>

jQuery js ファイルが含まれていることを確認してください。

于 2012-04-10T17:03:59.733 に答える
1

以下を使用して、フォームタグで ajax リクエストを送信する必要があります。

:リモート => 真

for_for メソッドに関するドキュメントを確認してください: FormHelper#form_for-instance_method

JQuery はデフォルトで rails 3.0 になっているので、問題なく実行できます。

于 2012-04-10T17:03:04.760 に答える