使用 Rails 3 单击编辑按钮后重定向到同一页面

Redirect to same page after click in edit button using Rails 3

我有一个表单,当用户提交表单时,所有提交的数据都出现在 form.In 下面的 table 这个 table 有一个编辑选项。我想要当用户再次单击该编辑选项时,该表单将显示所有 table 数据供用户编辑,用户将再次编辑并再次提交。

我在下面解释我的代码。

views/vendor/vendor_add.html.erb:

<div class="block block-themed">
    <div class="block-title">
        <div class="block-options">
            <a href="javascript:void(0)" class="btn btn-option enable-tooltip" data-toggle="block-collapse" title="Toggle block's content"><i class="fa fa-arrow-up"></i></a>
        </div>
            <h4>Vendor Details</h4>
    </div>
    <!--2nd_total_div-->
    <%= form_for :vendor,:url => {:action => "create_vendor" },remote: true do |f| %>
    <div class="block-content">
        <div class="timeline-container">
            <div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Registration No :</div></span>
            <%= f.text_field :reg_no ,:class => "form-control",:id => "reg" %>
              </div>
              <div class="input-group bmargindiv1 col-md-6 pull-left" id="dp1" data-date="12-02-2015" data-date-format="dd-mm-yyyy"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Date &amp; Time :</div></span>
              <%= f.text_field :dat ,:class => "form-control add-on",placeholder:"Date &amp; Time" %>
              </div>
            <div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Name :</div></span>
             <%= f.text_field :v_name ,:class => "form-control",placeholder:"Name" %>
              </div>
              <div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Father Name :</div></span>
              <%= f.text_field :f_name ,:class => "form-control",placeholder:"Father Name" %>
              </div>
              <div class="input-group bmargindiv1 col-md-6 pull-left" id="dp2" data-date="12-02-2015" data-date-format="dd-mm-yyyy"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Date of Birth :</div></span>
              <%= f.text_field :dob ,:class => "form-control add-on",placeholder:"Date of Birth" %>
              </div>
              <div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Mob. No :</div></span>
               <%= f.text_field :mob_no ,:class => "form-control",placeholder:"Mob. No" %>
              </div>
              <div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Address :</div></span>
               <%= f.text_field :v_address ,:class => "form-control",placeholder:"Address" %>
              </div>
              <div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Service Category :</div></span>
              <%= f.select(:s_catagory,options_for_select([['Wood','Wood'],['Puja Samagree','Puja Samagree'],['Sweeper','Sweeper'],['Photo Grapher','Photo Grapher'],['Burning Assistant','Burning Assistant']],selected: "Select Category"),{},{:class => 'form-control'}) %>
              </div>
              <div class="clearfix"></div>
              <div class="tbpaddingdiv1 text-center">
                <%= f.submit "Submit",:class => "btn btn-success" %>
              </div>
        </div>
    </div>
    <% end %>
    <!--end_2nd_total_div-->
</div>
<!--end_1st_total_div-->
<div class="block block-themed" id="vendor-total-div" style="display:none;" >
    <div class="block-title">
        <div class="block-options">
            <a href="javascript:void(0)" class="btn btn-option enable-tooltip active" data-toggle="block-collapse" title="Toggle block's content"><i class="fa fa-arrow-down"></i></a>
        </div>
            <h4>Vendor Details</h4>
    </div>
    <!--2nd_total_div-->
    <div id="vendor-div">
      </div
</div>
<!--end_2nd_total_div-->
</div>

views/vendor/create_vendor.js.erb:

$("#vendor-total-div").css("display", "block");
$("#vendor-div").html("<%= escape_javascript (render 'vendor_output_table' ) %>");
$("#vendor-div").slideDown(350);

controller/vendors_controller.rb:

class VendorsController < ApplicationController
    def vendor_add
        @vendor=Vendor.new
        respond_to do |format|
            format.html
            format.js
        end
    end
    def create_vendor
        @vendor=Vendor.new(params[:vendor])
        if @vendor.save
            flash[:notice]="Vendor data has submitted"
            flash[:color]="valid"
        else
            flash[:alert]="Data could not submit"
            flash[:color]="invalid"
            render 'vendor_add'
        end
    end
end

views/vendor/_vendor_output_table.html.erb:

<div class="block-content" style="display:none;">
        <div class="timeline-container">
          <div class="table-responsive">
                <table class="table table-bordered">
                <colgroup>
                    <col class="col-md-1 col-sm-1">
                    <col class="col-md-1 col-sm-1">
                    <col class="col-md-2 col-sm-2">
                    <col class="col-md-2 col-sm-2">
                    <col class="col-md-2 col-sm-2">
                    <col class="col-md-2 col-sm-2">
                    <col class="col-md-1 col-sm-1">
                    <col class="col-md-1 col-sm-1">
                </colgroup>
                <thead>
                    <tr>
                    <th class="text-center">Sl. No</th>
                    <th class="text-center">Date</th>
                    <th class="text-center">Registration No</th>
                    <th class="text-center">Name</th>
                    <th class="text-center">Mob. No</th>
                    <th class="text-center">Service Category</th>
                    <th class="text-center">Status</th>
                    <th class="text-center">Action</th>
                    </tr>
                </thead>
                <tbody>
                <tr>
                    <td class="text-center"><%= @vendor.id %></td>
                    <td class="text-center"><%= @vendor.dat %></td>
                    <td class="text-center"><%= @vendor.reg_no %></td>
                    <td class="text-center"><%= @vendor.v_name %></td>
                    <td class="text-center"><%= @vendor.mob_no %></td>
                    <td class="text-center"><%= @vendor.s_catagory %></td>
                    <td class="text-center"><%= @vendor.id %></td>
                    <td class="text-center">
                        <div class="btn-group">
                            <a href="#" class="btn btn-xs btn-success" data-toggle="tooltip" data-placement="top" title="Edit">
                                <i class="fa fa-edit"></i>
                            </a>
                        </div>
                    </td>
                </tr>
                </tbody>
                </table>
            </div>
        </div>
    </div>

选中此 table,在 table.I 的最后一行有一个编辑选项需要当用户单击该编辑选项时,只有表单会再次出现所有 table编辑数据和用户将编辑和更新 it.Please 帮助我。

在您的控制器中再创建两个方法,例如 vendor_editvendor_update。就像您使用 newcreateeditupdate 方法一样简单。

并更新 formroutes 等文件中的相应更改。