我有一个列表控制器。在许多地方调用控制器的创建操作。我想为ajax和html使用这个方法。目前我无法使它适用于ajax调用。虽然我在表单标签中有remote:true,但它正在发送正常的html请求。这是html和action的代码:
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>
routes.rb.................
post 'create_list' => 'lists#create'
这是我的服务器日志:
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?)
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();
}
})