Respond_to混乱

时间:2015-02-19 17:07:47

标签: javascript ruby-on-rails

我有一个编辑/更新表单,我通过form_for:

设置
<%= form_for(@vocab, url: user_vocab_path(@user, @vocab), remote: true)  do |f| %>
                <%= render 'shared/error_messages', object: f.object %>

                <%= f.text_field :front, class: 'form-control' %>
                <%= f.text_field :back, class: 'form-control' %>

                <%= f.submit "Save", class: "btn btn-large btn-primary" %>
                <%= link_to "Cancel", class: "btn btn-large" %>
<% end %>

如果是有效数据,我希望相应的更新操作保存更改,并将用户重定向到他来自的位置。在这种情况下不需要JS。如果数据无效,我希望JS处理这种情况。我将控制器设置为:

def update
    @user = current_user
    @vocab = @user.vocabs.find(params[:id])

    if @vocab.update_attributes(vocab_params)
      flash[:notice] = "Vocab updated!"
      respond_to do |format|
        format.js { redirect_back_or(home_user_path(@user)) }
      end
    else
      flash[:error] = "Vocab invalid!"
      respond_to do |format|
        format.html { render 'edit' }
        format.js   { render file: "vocabs/update.js" }
      end
    end
end 

我遇到的问题是,在有效数据的情况下,保存的URL(study_setup)正在被处理为JS,这可以防止该方被加载。 我在哪里告诉Rails使用JS进行此重定向?

Started PATCH "/users/1/vocabs/445" for 127.0.0.1 at 2015-02-19 18:03:16 +0100
Processing by VocabsController#update as JS
Parameters: {"utf8"=>"✓", "vocab"=>{"front"=>"kjhkkhsdsf1", "back"=>"jhgjhklj1"}, "commit"=>"Save", "user_id"=>"1", "id"=>"445"}
User Load (0.4ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1 LIMIT 1
User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
Vocab Load (0.2ms)  SELECT  "vocabs".* FROM "vocabs"  WHERE "vocabs"."user_id" = ? AND "vocabs"."id" = ?  ORDER BY created_at DESC LIMIT 1  [["user_id", 1], ["id", 445]]
(0.1ms)  begin transaction
Vocab Exists (0.3ms)  SELECT  1 AS one FROM "vocabs"  WHERE (LOWER("vocabs"."front") = LOWER('kjhkkhsdsf1') AND "vocabs"."id" != 445 AND "vocabs"."back" = 'jhgjhklj1' AND "vocabs"."user_id" = 1) LIMIT 1
SQL (0.3ms)  UPDATE "vocabs" SET "back" = ?, "updated_at" = ? WHERE "vocabs"."id" = 445  [["back", "jhgjhklj1"], ["updated_at", "2015-02-19 17:03:16.494941"]]
(2.3ms)  commit transaction
Redirected to http://localhost:3000/users/1/study_setup
Completed 302 Found in 17ms (ActiveRecord: 3.9ms)


Started GET "/users/1/study_setup" for 127.0.0.1 at 2015-02-19 18:03:16 +0100
Processing by UsersController#study_setup as JS
Parameters: {"id"=>"1"}
User Load (0.6ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1 LIMIT 1
User Load (0.1ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
(0.1ms)  begin transaction
User Exists (0.3ms)  SELECT  1 AS one FROM "users"  WHERE      (LOWER("users"."email") = LOWER('example@railstutorial.org') AND "users"."id" != 1) LIMIT 1
(0.1ms)  commit transaction
Tag Load (0.2ms)  SELECT "tags".* FROM "tags"  WHERE "tags"."user_id" = ? AND "tags"."active" = 't'  [["user_id", 1]]
Rendered users/_filter_form.html.erb (2.6ms)
(0.2ms)  SELECT COUNT(*) FROM "vocabs"  WHERE "vocabs"."user_id" = ?  [["user_id", 1]]
Vocab Load (0.2ms)  SELECT "vocabs".* FROM "vocabs"  WHERE "vocabs"."user_id" = ?  ORDER BY created_at DESC  [["user_id", 1]]
Rendered users/_vocab_list_for_selection.html.erb (2.1ms)
Rendered users/study_setup.html.erb within layouts/application (8.9ms)
Rendered layouts/_shim.html.erb (0.1ms)
Rendered layouts/_header.html.erb (1.1ms)
Completed 200 OK in 237ms (Views: 227.5ms | ActiveRecord: 1.7ms)

1 个答案:

答案 0 :(得分:1)

remote: true告诉Rails向您的服务器发送ajax请求。