I am trying to prepend
a partial view returned by an ajax request to a div content. I have been trying for quite some time now, but it does not seem to want to work.
我试图将ajax请求返回的部分视图添加到div内容。我已经尝试了很长一段时间,但它似乎并不想工作。
Here is my javascript (jquery) code:
这是我的javascript(jquery)代码:
$("#new_post_form").bind "ajax:success", (event, data, xhr) ->
$("#feed").prepend(data)
My controller will return a partial view when a post is created:
创建帖子时,我的控制器将返回部分视图:
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.js { render :partial => "posts/post", :locals => {:post => @post}}
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
Not sure why the data does not get added to the #feed. I believe the data is an object rather than a html string?
不确定为什么数据没有被添加到#feed。我相信数据是一个对象而不是一个html字符串?
Any help is appreciated. Thanks.
任何帮助表示赞赏。谢谢。
Extra: Form that calls ajax
额外:调用ajax的表单
=simple_form_for(@post, :method => "post", :remote => true, :id => "new_post_form") do |f|
=f.error_notification
=f.text_area :description, :id => "post_box", :class => "span12"
%br
%div{:id=>"post_box_buttons"}
.input-append
=f.select("location_id", @college.locations.collect {|r| [ r.name, r.id ] }, { :include_blank => false })
=f.button :submit, :value => "Submit", :class => "btn btn-primary"
1 个解决方案
#1
0
simple_form_for
doesn't add your desired id
that way. You need:
simple_form_for不会以这种方式添加您想要的ID。你需要:
=simple_form_for(@post, :method => "post", :remote => true, :html => {id: 'new_post_form'}) do |f|
= simple_form_for(@ post,:method =>“post”,:remote => true,:html => {id:'new_post_form'})do | f |
#1
0
simple_form_for
doesn't add your desired id
that way. You need:
simple_form_for不会以这种方式添加您想要的ID。你需要:
=simple_form_for(@post, :method => "post", :remote => true, :html => {id: 'new_post_form'}) do |f|
= simple_form_for(@ post,:method =>“post”,:remote => true,:html => {id:'new_post_form'})do | f |