Ruby on rails 远程:true不适用于响应格式为html和js的控制器操作
我有一个列表控制器。控制器的创建操作在许多地方被调用。我想在ajax和html中使用这种方法。目前,我无法使其适用于ajax调用。虽然我在表单标记中有remote:true,但它正在发送普通的html请求。以下是html和操作的代码:Ruby on rails 远程:true不适用于响应格式为html和js的控制器操作,ruby-on-rails,ajax,ruby-on-rails-5,Ruby On Rails,Ajax,Ruby On Rails 5,我有一个列表控制器。控制器的创建操作在许多地方被调用。我想在ajax和html中使用这种方法。目前,我无法使其适用于ajax调用。虽然我在表单标记中有remote:true,但它正在发送普通的html请求。以下是html和操作的代码: list_controller#create.................................. def create @list = current_user.list.new(list_params) current_
list_controller#create..................................
def create
@list = current_user.list.new(list_params)
current_user.list << @list
respond_to do |format|
if @list[:name].strip.blank?
format.html { redirect_to articles_url, notice: "List name can't be blank." }
format.js { }
elsif @list.save
format.html { redirect_to articles_url, notice: 'List was successfully created.' }
format.js { }
end
end
end
html form............................................
<%= form_for List.new, url: create_list_path,remote: true, authenticity_token: true, html: { class: 'form-horizontal', style: 'display:block;width: 240px;' } do |f| %>
<div class="form-group donotchange">
<%= f.text_field :name, class: 'col-xs-8 form-control', placeholder: 'List name', required: true, autocomplete: :off %>
</div>
<div class="form-group donotchange text-center">
<%= f.submit 'Create list', class: "btn btn-success col-xs-12" %>
</div>
这是我的服务器日志:
Started POST "/create_list" for 127.0.0.1 at 2017-08-14 19:21:06 +0545
Processing by ListsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"R8g8N+b/g3Fcn8SOOHWGAYtfEKoRUS8Em8NgjcjtsDppaZMnWxT4DpzFHurgyWi0PJgvELLfGTc2I5g5ttMYOg==", "list"=>{"name"=>"my list"}, "commit"=
>"Create list"}
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
(1.0ms) BEGIN
以下是请求头:(为什么仍然是text/html?)
我把这张表格放在了一件府绸里。以下是代码:
This is placeholder for form(this is inside a modal)...........
<label for="add_to_existing_list">Add to Existing List</label>
<a class="pull-right" rel="popover" data-content='' data-placement="right">Create new list</a>
This is the form...........
<div id="popover-content" style="display: none">
<%= form_for List.new, url: create_list_path,remote: true, authenticity_token: true, html: { class:
'form-horizontal', style: 'display:block;
width: 240px;
' } do |f| %>
<div class="form-group donotchange">
<%= f.text_field :name, class: 'col-xs-8 form-control', placeholder: 'List name', required: true, autocomplete: :off %>
</div>
<div class="form-group donotchange text-center">
<%= f.submit 'Create list', class: "btn btn-success col-xs-12" %>
</div>
<% end %>
</div>
This is the js that loads the popover...........
$('a[rel=popover]').popover({
html: 'true',
placement: 'right',
content : function() {
return $('#popover-content').html();
}
})
这是表单的占位符(它在模态中)。。。。。。。。。。。
添加到现有列表
创建新列表
这是表格。。。。。。。。。。。
这是加载popover的js。。。。。。。。。。。
$('a[rel=popover]')。popover({
html:'真',
位置:'对',
内容:函数(){
返回$('#popover content').html();
}
})
您可以发布服务器日志吗?我已经添加了日志,请check@Pavan你得到什么了吗?没有。看起来很奇怪。看看这个答案:试着调试你的代码,看看问题出在哪里。你能发布服务器日志吗?我已经添加了日志check@Pavan你得到什么了吗?没有。看起来很奇怪。看看这个答案:试着调试你的代码,看看问题出在哪里。
Accept
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding
gzip, deflate
Accept-Language
en-US,en;q=0.5
Connection
keep-alive
Content-Length
175
This is placeholder for form(this is inside a modal)...........
<label for="add_to_existing_list">Add to Existing List</label>
<a class="pull-right" rel="popover" data-content='' data-placement="right">Create new list</a>
This is the form...........
<div id="popover-content" style="display: none">
<%= form_for List.new, url: create_list_path,remote: true, authenticity_token: true, html: { class:
'form-horizontal', style: 'display:block;
width: 240px;
' } do |f| %>
<div class="form-group donotchange">
<%= f.text_field :name, class: 'col-xs-8 form-control', placeholder: 'List name', required: true, autocomplete: :off %>
</div>
<div class="form-group donotchange text-center">
<%= f.submit 'Create list', class: "btn btn-success col-xs-12" %>
</div>
<% end %>
</div>
This is the js that loads the popover...........
$('a[rel=popover]').popover({
html: 'true',
placement: 'right',
content : function() {
return $('#popover-content').html();
}
})