编辑字符串时,它的值会在视觉上发生变化,但是当我刷新页面时,该值将变为之前的值。 我正在使用bootstrap-x-editable-rails 1.5.1.1 gem进行编辑。
控制器代码:
module Admin
class DoctypesController < AdminController
before_action :set_doctype, only: [:show, :edit, :destroy, :update]
def index
@doctypes=DocumentType.order("title").page(params[:page])
end
def show
end
def new
@doctype = DocumentType.new
end
def create
@doctype=DocumentType.create(doctype_params)
redirect_to admin_doctypes_path
end
def destroy
@doctype.destroy
redirect_to admin_doctypes_path
end
def update
@doctype.update(doctype_params)
redirect_to admin_doctypes_path
end
def edit
end
def set_doctype
@doctype = DocumentType.find(params[:id])
end
private
def doctype_params
params.require(:document_type).permit(:id,:title)
end
end
end
Index.html.erb代码:
<tbody>
<% @doctypes.each do |doctype| %>
<tr>
<td class="doctype_edit" data-model="document_type" data-type="text" data-url=<%=edit_admin_doctype_path(doctype)%> ><%= doctype.title%></td>
<td>
<%= link_to '', :id => 'btn_remove_doctype', :data => { :path => admin_doctype_path(doctype), :toggle => 'modal', :target => '#modal_confirm_remove_doctype' }, :class => "btn btn-danger btn-xs" do %>
<span class='glyphicon glyphicon-remove'></span>
<% end %>
</td>
</tr>
<% end %>
</tbody>
JS代码:
$.fn.editable.defaults.mode = 'inline';
$.fn.editable.defaults.ajaxOptions = {type: "GET"};
$('.doctype_edit').editable();
部分路线:
admin_doctypes GET /admin/doctypes(.:format) admin/doctypes#index
POST /admin/doctypes(.:format) admin/doctypes#create
new_admin_doctype GET /admin/doctypes/new(.:format) admin/doctypes#new
edit_admin_doctype GET /admin/doctypes/:id/edit(.:format) admin/doctypes#edit
admin_doctype GET /admin/doctypes/:id(.:format) admin/doctypes#show
PATCH /admin/doctypes/:id(.:format) admin/doctypes#update
PUT /admin/doctypes/:id(.:format) admin/doctypes#update
DELETE /admin/doctypes/:id(.:format) admin/doctypes#destroy
答案 0 :(得分:2)
这就是它的工作方式。
控制器:
def update
if @doctype.update(title: params[:value])
status = msg = 'ok'
else
status = 'error'
msg = @doctype.errors.full_messages.join(',')
end
if request.xhr?
render :json => {
:status => status,
:msg => msg
}
end
end
HTML:
<% @doctypes.each do |doctype| %>
<tr>
<td>
<div class="doctype_edit" data-type="text" data-url="/admin/doctypes/<%=doctype.id%>" data-name="title" data-pk=<%= doctype.id %> >
<%= doctype.title%>
</div>
</td>
<td>
<%= link_to '', :id => 'btn_remove_doctype', :data => { :path => admin_doctype_path(doctype), :toggle => 'modal', :target => '#modal_confirm_remove_doctype' }, :class => "btn btn-danger btn-xs" do %>
<span class='glyphicon glyphicon-remove'></span>
<% end %>
</td>
</tr>
<% end %>
Js代码:
$.fn.editable.defaults.mode = 'inline';
$.fn.editable.defaults.ajaxOptions = {type: "PATCH"};
$('.doctype_edit').editable({
inputclass: 'inputdoctype',
success: function(response, newValue) {
if(response.status == 'error') {
return response.msg;
} else {
$('this').html(newValue);
}
}
});