简体   繁体   English

带有新控制器动作导轨的Ajax

[英]Ajax with new controller action rails

I have created a new micropost controller action called more to receive ajax request. 我创建了一个名为more的新的micropost控制器动作,以接收ajax请求。

def more
  micropost=Micropost.find_by(params[:id])
  @answers=micropost.answers
  respond_to do |format|
   format.html {redirect_to micropost}
   format.js
  end
end

and I have created jquery file- more.js.erb 并且我创建了jquery文件-more.js.erb

$(".microposts").html("<%= escape_javascript(render('users/unfollow')) %>"); `

to replace the content with a partial my route file is like 用部分路由文件替换内容就像

resources :microposts, only: [:edit,:create,:destroy,:update,:show,:more] do
  member do
    get :more
  end
end

and i call the javascript file in the view with 我在视图中调用javascript文件

<%= link_to "load more",more_micropost_path(micropost),remote: true %>

Its working with normal html request but not ajax.Nothing happens when I click on the link. 它适用于普通的html请求,但不适用于ajax。当我单击链接时没有任何反应。 I saw similar questions asked by some but the fixes are not working for me. 我看到一些人提出了类似的问题,但修复程序对我不起作用。 Can someone help me with this. 有人可以帮我弄这个吗。 Thanks in advance.. The error in firebug console is 在此先感谢.. Firebug控制台中的错误是

`500 internal server error. `500内部服务器错误。 NoMethodError in MicropostsController#more. MicropostsController#more中的NoMethodError。 Undefined method id for nil:NilClass' nil:NilClass'的未定义方法ID

The firebug error console shows this jquery line on the right hand side. firebug错误控制台在右侧显示此jquery行。 I am not sure if its an error. 我不确定是否有错误。

lixhr.send( ( options.hasContent && options.data ) || null ); lixhr.send((options.hasContent && options.data)|| null);

judging by the error you posted in the comments, this line may be a problem: 从您在评论中发布的错误来看,此行可能是一个问题:

micropost = Micropost.find_by(params[:id])

find_by requires a hash, so either use find find_by需要一个哈希,因此可以使用find

micropost = Micropost.find(params[:id])

or 要么

micropost = Micropost.find_by(id: params[:id])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM