Rails 3.2アプリケーション用のAjaxを使用して更新フォームを作成しようとしています。私はAjax初心者ですので、いくつか指導が必要です。私は何を達成したいことは次のとおりです。AjaxによるRails 3.2の更新アクション。
1. The user presses a button and a form slides down from the top in a div.
2. The user updates the form and presses "update".
3. The parameters from the updated form is sent and saved to the database, and
4. the form slides up again without making the user leave the page.
フォームは現在、次のようになります。これは、に位置しています
def update
@user = current_user
@user.update_attributes(params[:user])
end
:私は現在、このようになります更新アクションを持っている
<%= form_for(@user, :remote => true) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.fields_for :oak do |oak| %>
<%= oak.text_field :name, :placeholder => "Name" %>
<% end %>
<%= f.fields_for :energy_meter do |energy_meter| %>
<%= energy_meter.text_field :energy_meter_id, :placeholder => "Id" %>
<% end %>
<%= f.submit "Update!", "data-role" => "button", "data-theme" => "b", "return" => "false" %>
<% end %>
私のUsersController。私は私がやりたいupdate.js.erbを入れて、またどのようにすべきかわからない
$(window.location.replace("/"))
:
は、私はまた、現在、このようになりますupdate.js.erbファイルを持っていますパラメータを送信すると、現在は/
にリダイレクトされます。私はまた、私は自分の更新アクションに入れなければならないものは何か、それは現在UsersControllerに位置しているが、それは私のフォームがどこにある私のPagesControllerに置かれるべきか?
ヒントやガイダンスは素晴らしいと思います!
ありがとうございました。
こんにちは、表示するようにアラートを取得しました。しかし、それは/ users/1へのリダイレクトです。私の更新アクションは、次のようになります: 'def update @user = current_user respond_to do | format | @ user.update_attributes(params [:user])format.js {render:layout => false} end end'です。定義されていないテキストが表示されます。 – Anders
'application.js'に正しい' form id'を指定していることを確認してください。また、フォームが送信されるときにどのリクエスト(JSまたはHTML)が送信されているかをコンソールでチェックします。 – Bongs