如何在不使用javascript刷新rails中的页面的情况下添加和删除

时间:2016-08-09 10:18:59

标签: javascript ajax reload ruby-on-rails-5

我是rails的新手。我想做的是,如果我想在不刷新页面的情况下添加和删除数据。

我已经使用了remote => true它正在工作。但是一旦我将刷新页面数据就会显示它未正确存储的数据。告诉我如何在不刷新页面的情况下添加数据。

Controller.rb

 def attribute_creation
    if $classification_id==nil
       $classification_id=Classification.last.id
    end
   @field_name=[]
   @field_type=[]
   field_id=FieldProcess.where(:tree_status=>0).where(:classification_id=>$classification_id).pluck(:field_id)
   field_id.map do |field|
     @field_name << FieldMaster.where(:id=>field).to_a
     @field_type << FieldProcess.where(:field_id=>field).to_a      
   end
     @field=FieldMaster.new
     @field_name1=@field_name.flatten!
     @field_type1=@field_type.flatten!
 end

Attribute_creation.html.erb

<table>
  <thead>
     <tr>
       <th>Field Name</th>
       <th>Field Type</th>
       <th>Remove</th>
     </tr>
</thead>
<%unless @field_name1.nil?%>
  <%@field_name1.each do |field|%>
    <tr>
      <td><%= field.field_name%></td>
      <td><%= FieldProcess.where(:field_id=>field.id).to_a[0].field_type %></td>
      <td><%= link_to '<i class="fa fa-trash" aria-hidden="true"></i>'.html_safe, images_destroy_field_path(field.id), data: { confirm: 'Are you sure?' }%></td>
    </tr>
  <%end%>
<%end%> 
<%= form_for FieldMaster.new, :remote => true,:url=>{:controller=>"images", :action=>"create_attribute",remote: true} do|m|%>
  <tr>
    <td> <%= m.select "id",options_from_collection_for_select(FieldMaster.order("field_name ASC"), "id", "field_name"),{:include_blank=>"Select Option"},:class=>"form-control",:required=>"true"%></td>
    <td><%= m.select :field_type, ['Text', 'Date', 'Dropdown'], {:include_blank=>"select Option"},:class=>"form-control",:id=>"smcCompny" %></td>
    <td><%= m.submit "Add",:class=>"btn btn-primary"%></td>
  </tr>
<%end%>

1 个答案:

答案 0 :(得分:0)

这可以使用Rails Ajax方法来完成 以下链接可以帮助您。

<强> Working with JavaScript in Rails