让另一个控制器查看部分内容 (Rails 3)
有评论的帖子。在帖子/节目中,当用户单击“添加评论”按钮时,服务器会调用一个 javascript 函数,该函数应将新评论操作添加为部分内容:
render 'comments/new'
$("#newcomment").live("click",function() { $("#addcomment").load("<%= url_for :controller => 'comments', :action => 'new', :locals => {:parent_id => @post.parent_id} %>")
def new
@comment = Comment.new( :parent_id => params[:parent_id] )
render :partial => "form", :layout => false
end
new view:
render "form" # form is the add comment form
问题是局部变量未传递,我无法添加评论(它不会调用创建)
Have a post with comments. On post/show, when a user clicks the add comment button the server calls a javascript function that should add the new comment action as a partial:
render 'comments/new'
$("#newcomment").live("click",function() { $("#addcomment").load("<%= url_for :controller => 'comments', :action => 'new', :locals => {:parent_id => @post.parent_id} %>")
def new
@comment = Comment.new( :parent_id => params[:parent_id] )
render :partial => "form", :layout => false
end
new view:
render "form" # form is the add comment form
The problem is that the local variables are not passed and I can't add the comment (it wont call create)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
render *
永远不会在控制器中调用操作,即使render :action =>; ‘编辑’!它在每种情况下仅生成相应的部分。有时可能会令人困惑。
为了快速解决问题,您可以做的就是通过 ajax 调用来调用您的部分。
在您的意见中:
在您的控制器中:
希望这有帮助。
render *
never call an action in a controller, evenrender :action => 'edit'
! It generates only the corresponding partial in every case. It may sometimes be confusing.What you cand do to solve your probelm quickly is to call your partial via an ajax call.
In your views :
In your controller :
Hope this help.