Javascript Rails 5:更新操作不适用于AJAXified表单
我有一个下拉视图,在用户选择时,可以编辑特定的用户角色 在/common/roles/index.erb中,我有:Javascript Rails 5:更新操作不适用于AJAXified表单,javascript,jquery,ruby-on-rails,ajax,Javascript,Jquery,Ruby On Rails,Ajax,我有一个下拉视图,在用户选择时,可以编辑特定的用户角色 在/common/roles/index.erb中,我有: <form class="form-horizontal" role="form" method="get" action="/common/roles" data-remote="true"> <div class="col-sm-10"><select class="form-control m-b" name="users" id="user_l
<form class="form-horizontal" role="form" method="get" action="/common/roles" data-remote="true">
<div class="col-sm-10"><select class="form-control m-b" name="users" id="user_list">
<option value="">Please, select user</option>
<% @users.each do |user| %>
<% for role in user.roles %>
<option value="<%= role.id %>"><%= user.name %></option>
<% end %>
<% end %>
</select>
</div>
</form>
<!-- Here we render role edit form -->
<div id = 'selected_role' ></div>
<!-- Role edit form ends -->
def update
@role = Role.find(params[:id])
if @role.update_attributes(role_params)
respond_to do |format|
format.html {
flash[:success] = "Role updated!"
redirect_to common_roles_path
}
format.js
/common/roles/edit.js.erb如下所示:
$("#selected_role").html("<%=j render 'common/roles/editrole', locals: { role: @role } %>");
尽管有特定的补丁路径:
common_role PATCH /common/roles/:id(.:format) common/roles#update
在/common/roles_controller.rb中,我有以下内容:
<form class="form-horizontal" role="form" method="get" action="/common/roles" data-remote="true">
<div class="col-sm-10"><select class="form-control m-b" name="users" id="user_list">
<option value="">Please, select user</option>
<% @users.each do |user| %>
<% for role in user.roles %>
<option value="<%= role.id %>"><%= user.name %></option>
<% end %>
<% end %>
</select>
</div>
</form>
<!-- Here we render role edit form -->
<div id = 'selected_role' ></div>
<!-- Role edit form ends -->
def update
@role = Role.find(params[:id])
if @role.update_attributes(role_params)
respond_to do |format|
format.html {
flash[:success] = "Role updated!"
redirect_to common_roles_path
}
format.js
在/common/roles/update.js.erb中,我有一行:$('.editrole').hide()
我可以在编辑中打开表单,直到更新操作正常为止。
请问如何修复更新操作错误?
解决方案更新
在更新操作中,我缺少respond_to do | format |
+/common/roles/Update.js.erb,我必须更改为:
$('.container').empty().append('<%=
escape_javascript(
render 'update'
)
%>')
$('.container').empty().append('')
试试这个
<%= form_for role, url: common_role_path(role), method: :patch, remote: true do |f| %>
role
这里指的是在呈现分部时传递的局部变量。试试这个
<%= form_for role, url: common_role_path(role), method: :patch, remote: true do |f| %>
role
这里指的是在呈现部分时传递的局部变量。谢谢,这会产生错误:ActionView::Template::error(未定义的局部变量或方法
role'`和编辑表单未加载。但是,如果我加载了
,它将使用正确的url启动修补程序,但会出现以下错误:已完成500个内部服务器错误
已接近解决方案,但还没有……可能有什么问题?+错误后面还有几行:ArgumentError(论据太少):
app/controllers/common/roles\u controller.rb:54:in format
app/controllers/common/roles\u controller.rb:54:in update
您正在传递局部变量role
中的@role
对吗?是的,没错。看起来控制器的更新操作在format.html中有问题。它要求不同的nt参数。基本上,我希望在角色更新后返回到索引并闪存成功更新该角色。@Matis您需要添加format.js
,默认情况下,它将查找update.js.erb
。创建update.js.erb
并在其中添加javascript响应谢谢,这会产生错误:ActionView::Template::错误(未定义的局部变量或方法
role'`和编辑表单未加载。但是,如果我这样做,
它将使用正确的url启动修补程序,但会出现此错误:已完成500个内部服务器错误
已接近解决方案,但还没有…可能有什么问题?+错误后面还有几行:ArgumentError(论据太少):
app/controllers/common/roles\u controller.rb:54:in format
app/controllers/common/roles\u controller.rb:54:in update
您正在传递局部变量role
中的@role
对吗?是的,没错。看起来控制器的更新操作在format.html中有问题。它要求不同的nt参数。基本上,我希望在角色更新后返回索引并闪存成功,该角色已更新。@Matis您需要添加format.js
,默认情况下,它将查找update.js.erb
。创建update.js.erb
并在其中添加javascript响应