Make another controllers view a partial (Rails 3)

和自甴很熟 提交于 2019-12-13 03:32:09

问题


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)


回答1:


render * never call an action in a controller, even render :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 :

<div id="new_comment"></div>

In your controller :

render :update do
    replace 'news_comment', partial => 'comments/new'
end

Hope this help.



来源:https://stackoverflow.com/questions/5941697/make-another-controllers-view-a-partial-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!