我正在尝试prepend
ajax请求返回到div内容的部分视图。我现在已经尝试了很长一段时间,但它似乎并不想工作。
这是我的javascript(jquery)代码:
$("#new_post_form").bind "ajax:success", (event, data, xhr) ->
$("#feed").prepend(data)
我的控制器将在创建帖子时返回部分视图:
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
不确定为什么数据没有添加到#feed。我相信数据是一个对象而不是一个html字符串?
感谢任何帮助。谢谢。
额外: 调用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"
答案 0 :(得分:0)
simple_form_for
不会以这种方式添加您想要的id
。你需要:
=simple_form_for(@post, :method => "post", :remote => true, :html => {id: 'new_post_form'}) do |f|