使另一个控制器查看部分(Rails 3) [英] Make another controllers view a partial (Rails 3)

查看:47
本文介绍了使另一个控制器查看部分(Rails 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发表有评论的帖子.在发布/展示时,当用户单击添加评论按钮时,服务器会调用一个 javascript 函数,该函数应该将新评论操作作为部分添加:

render 'comments/new'$("#newcomment").live("click",function() { $("#addcomment").load("<%= url_for :controller => 'comments', :action => 'new', :locals => {:parent_id => @post.parent_id} %>")定义新@comment = Comment.new(:parent_id => params[:parent_id])渲染:部分=>"形式", :layout =>错误的结尾新观点:render "form" # 表单是添加评论表单

问题是局部变量没有通过,我无法添加注释(它不会调用create)

解决方案

render * 从不调用控制器中的动作,即使 render :action =>'编辑'!它在每种情况下只生成相应的部分.有时可能会令人困惑.

您可以通过ajax调用快速解决问题的方法是调用partial.

在您看来:

在您的控制器中:

render :update do替换'news_comment',部分=>'评论/新'结尾

希望对您有所帮助.

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)

解决方案

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.

这篇关于使另一个控制器查看部分(Rails 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆