Ruby on rails 3.1 Rails 3.1-访问控制器的最佳方式是什么;coffeescript中的变量?

Ruby on rails 3.1 Rails 3.1-访问控制器的最佳方式是什么;coffeescript中的变量?,ruby-on-rails-3.1,coffeescript,Ruby On Rails 3.1,Coffeescript,我正在寻找一种将控制器的全局变量用于咖啡脚本的方法,如下所示: 控制器: respond_to :js def create @commentable = commentable @comment = @commentable.comments.build({:comment => params[:comment], :user => current_user}) @comment.save respond_with(@comment, @commentable)

我正在寻找一种将控制器的全局变量用于咖啡脚本的方法,如下所示:

控制器:

respond_to :js

def create
  @commentable = commentable
  @comment = @commentable.comments.build({:comment => params[:comment], :user => current_user})
  @comment.save
  respond_with(@comment, @commentable)
end
create.js.coffee:

$("#form_#{@commentable.class}_#{@commentable.id}").hide()
jQuery ->

  commentable_link_id = null
  commentable_form_id = null

  hide_element_by_id = (id_name) ->
    $("#"+id_name).hide()
  show_element_by_id = (id_name) ->
    $("#"+id_name).show()

  $('.add_comment_link').click ->
    currentId = $(this).attr('id')
    if(commentable_link_id != null && commentable_form_id != null)
      hide_element_by_id(commentable_form_id)
      show_element_by_id(commentable_link_id)

    commentable_link_id = currentId
    commentable_form_id = currentId.replace("link", "form")    
    hide_element_by_id(commentable_link_id)
    show_element_by_id(commentable_form_id)

    commentable_comments_id = currentId.replace("link", "comments")   
    false

  $('.submit_comment').click -> 
    if(commentable_link_id != null && commentable_form_id != null)
      hide_element_by_id(commentable_form_id)
      show_element_by_id(commentable_link_id)
其思想是在一个页面中有多个注释,并识别当前被注释的注释,以隐藏/显示对其进行注释的表单;表单的id是使用类和可注释对象的id构建的。此时,如果我尝试使用上面的代码访问一个元素,它将不起作用,因为脚本中似乎不存在@commentable

编辑:

我读了答案,并尝试了以下方法:

在我的帖子/show.haml中

:javascript
  var commentable_comments_id;
#post
  @post.body
= render :partial => 'comments/list', :locals => {:commentable => @post}
在我的部分列表中

#comments_container
  %div{:id => "comments_#{commentable.class}_#{commentable.id}"}
    - commentable.comments.reverse_each do |comment|
      = render :partial => 'comments/comment', :locals => {:comment => comment}
  - if current_user
    .add_comment_link{:id => "link_#{commentable.class}_#{commentable.id}"}
      #{link_to "Commenter"}
    .add_comment{:id => "form_#{commentable.class}_#{commentable.id}"}
      = render :partial => 'comments/comment_form', :locals => {:commentable => commentable}
并以部分注释形式.haml

.comment_form
  = form_tag polymorphic_path([commentable, Comment]) , :method => :post, :remote => true do |f|
    .comment_field
      = text_area_tag :comment, params[:comment], :id =>"comment_area", :rows => 4, :cols => 50
    .comment_field 
      = submit_tag "Commenter", :id => "submit_comment_#{commentable.class}_#{commentable.id}", :class => "submit_comment"
在posts.js.coffee中:

$("#form_#{@commentable.class}_#{@commentable.id}").hide()
jQuery ->

  commentable_link_id = null
  commentable_form_id = null

  hide_element_by_id = (id_name) ->
    $("#"+id_name).hide()
  show_element_by_id = (id_name) ->
    $("#"+id_name).show()

  $('.add_comment_link').click ->
    currentId = $(this).attr('id')
    if(commentable_link_id != null && commentable_form_id != null)
      hide_element_by_id(commentable_form_id)
      show_element_by_id(commentable_link_id)

    commentable_link_id = currentId
    commentable_form_id = currentId.replace("link", "form")    
    hide_element_by_id(commentable_link_id)
    show_element_by_id(commentable_form_id)

    commentable_comments_id = currentId.replace("link", "comments")   
    false

  $('.submit_comment').click -> 
    if(commentable_link_id != null && commentable_form_id != null)
      hide_element_by_id(commentable_form_id)
      show_element_by_id(commentable_link_id)
因此,当用户单击链接添加评论时,它会隐藏以前的评论表单(如果有),显示新的正确表单,构建评论容器的id(例如comments_Post_3),并将其存储在页面的全局js变量中:

commentable_comments_id = currentId.replace("link", "comments")
然后在create.js.coffee中,我尝试使用以下变量将新注释附加到存储容器中:

$('<%= escape_javascript(render(:partial => @comment))%>')
  .appendTo("#"+commentable_comments_id)
  .hide()
  .fadeIn()
$('@comment))%>)
.appendTo(“#”+可注释的注释id)
.hide()
.fadeIn()

我认为这是不正确的,因为最后一个操作(带淡入淡出的附加)不起作用,所以不能初始化全局变量commentable\u comments\u id或其他…

如果要在coffeescript文件中使用erb,则必须将erb扩展名附加到该文件。因此,您的文件应该重命名为
create.js.coffee.erb
。然后你可以做一些类似的事情:

$ ->
  alert('<%= Post.first.name %>')
$->
警报(“”)
它应该很好用。有关更多信息,请参阅

编辑


我想你的问题我漏掉了一点。如果您想访问js文件中的实例变量,不幸的是,您不能。有关更多信息,请参阅答案

当您响应js时,这意味着它将
create.js.coffee
呈现为一个视图。您无需在末尾添加
.erb
即可在内部使用erb代码,如下所示:

$("#form_<%=@commentable.class%>_<%=@commentable.id>").hide()
你可以用电脑来做

只需将其添加到您的文件:

gem 'coffeebeans'
然后根据需要创建文件:

#app/views/posts/create.js.coffee
$("#form_<%= @commentable.class %>_<%= @commentable.id %>").hide()
#app/views/posts/create.js.coffee
$(“#形式"”).hide()

在阅读了您的链接后,我对问题进行了更精确的编辑。谢谢你的回复。这不是真的。提交远程表单时,无需添加erb扩展。问问桑南就知道了。这是伟大的,它的工作完全符合我的预期。只有一件事:上面的代码在我的例子中不起作用,因为要识别的id的格式是
#form#u Post_3
,所以我需要使用
$(“#form#u”).hide()。