通过ajax呈现的部分提交按钮不会重定向,使用rails 3.2

时间:2012-09-18 19:47:39

标签: jquery ruby-on-rails ajax redirect partial

我遇到了这个问题,我可以通过单击单选按钮来呈现部分视图,但是当我单击部分内部的提交按钮以创建新用户时,用户已成功创建,但表单未重定向到在用户页面中,它只是在后台创建一个新用户,因此您只能在刷新页面后看到新用户。有没有人知道怎么做到这一点当我点击提交按钮它会重定向到一个页面?

index.html.erb

Listing users
<table>
  <tr>
    <th>Name</th>
    <th>Email</th>
    <th></th>
    <th></th>
    <th></th>
</tr>
<% @users.each do |user| %>
  <tr>
    <td><%= user.name %></td>
    <td><%= user.email %></td>
    <td><%= link_to 'Show', user %></td>
    <td><%= link_to 'Edit', edit_user_path(user) %></td>
    <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>
<%= form_tag({:action => 'preview'}, :remote => true, :'data-update-target' => 'update-container') do %>
<%= label_tag :gender, 'male' %>
<%= radio_button_tag :gender, 'male', false, :class => 'submit' %><br/>
<%= label_tag :gender, 'female' %>
<%= radio_button_tag :gender, 'female', false, :class => 'submit' %>
<%=# submit_tag 'Submit', :disable_with => 'Please wait...', :class => 'submit' 
%>


users_controller.rb

class UsersController < ApplicationController
def index
  @users = User.all
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @users }
  end
end
def show
  @user = User.find(params[:id])
  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @user }
  end
end
def new
  @user = User.new
  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @user }
  end
end
def edit
  @user = User.find(params[:id])
end
# POST /users
# POST /users.json
def create
  @user = User.new(params[:user])
  respond_to do |format|
    if @user.save
      format.html { redirect_to @user, notice: 'User was successfully created.' }
      format.json { render json: @user, status: :created, location: @user }
    else
      format.html { render action: "new" }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end
# PUT /users/1
# PUT /users/1.json
def update
  @user = User.find(params[:id])
  respond_to do |format|
    if @user.update_attributes(params[:user])
      format.html { redirect_to @user, notice: 'User was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
  @user = User.find(params[:id])
  @user.destroy
  respond_to do |format|
    format.html { redirect_to users_url }
    format.json { head :no_content }
  end
end
def preview
  if params[:gender] == "male"
    @userd = User.new
    render :partial => 'preview', :object => @userd
  elsif params[:gender] == "female"
    @userd = User.new
    render :partial => 'preview', :object => @userd
  end
end
end

_preview.html.erb

<%= form_for(@userd, remote: true) do |f| %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

和我的application.js

$(function() {
/* Convenience for forms or links that return HTML from a remote ajax call.
The returned markup will be inserted into the element id specified.
*/
  $('form[data-update-target]').live('ajax:success', function(evt, data) {
    var target = $(this).data('update-target');
    $('#' + target).html(data);
  });
  $('input.submit').click(function(){ $(this).parent().submit(); return true; });
  });

0 个答案:

没有答案