开发者

Make another controllers view a partial (Rails 3)

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 开发者_如何学Goproblem is that the local variables are not passed and I can't add the comment (it wont call create)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜