【问题标题】:Render local variables in partials with Ajax, Rails 4使用 Ajax、Rails 4 渲染局部变量
【发布时间】:2015-09-09 02:23:45
【问题描述】:

在 Ajax 调用之后,我无法让局部变量显示在局部变量中。这是我的设置:

查看 - index.html.erb

<% @artists.each do |artist| %>
  <div class="index_follow_button">
    <%= render "partials/index_follow_button", :artist => artist %>
  </div>
<% end %>

查看 partials/_index_follow_button.html.erb

<% if fan_signed_in? %>
  <% if fan_signed_in? && current_fan.following_artist?(artist) %>
    <%= button_to "unfollow", artist_relationship_path(artist, current_fan.artist_relationship_id(artist)), method: :delete, remote: true %>
  <% elsif fan_signed_in? %>
    <%= form_for(ArtistRelationship.new, url: artist_relationships_path(artist), remote: true) do |f| %>
      <%= f.submit "follow" %>
    <% end %>
  <% end %>
<% end %>

查看 - follow_button.js.erb

$('.index_follow_button').html('<%=  escape_javascript(render :partial => 'partials/index_follow_button.html.erb', :locals => {:artist => artist}) %>');

我相信这是错误所在

控制器 - relationships_controller.rb

def create
  @relationship = ArtistRelationship.new
  @relationship.fan_id = current_fan.id
  @relationship.artist_id = Artist.friendly.find(params[:artist_id]).id
  @artist = Artist.friendly.find(params[:artist_id])
  if @relationship.save
    respond_to do |format|
      format.html { redirect_to (:back) }
      format.js { render :action => "follow_button" }
    end
  else
    redirect_to root_url
  end
end

def destroy
  current_fan.unfollow_artist(Artist.friendly.find(params[:artist_id]))
  @artist = Artist.friendly.find(params[:artist_id])
  respond_to do |format|
    format.html { redirect_to (:back) }
    format.js { render :action => "follow_button" }
  end
end

在索引页面中,使用:artist =&gt; artist 的部分显示完全正常,但在 JavaScript 页面中没有发生同样的事情,但我一直收到同样的错误 -

ActionView::Template::Error (undefined local variable or method `artist' for #<#<Class:0x3798df8>:0x6f43968>)

有什么想法吗?

【问题讨论】:

    标签: jquery ruby-on-rails ruby ajax ruby-on-rails-4


    【解决方案1】:

    Artist 是一个局部变量,但在您的 js 文件中它是未设置的(除非您遗漏了一些代码)。而是尝试使用实例变量: follow_button.js.erb:

    $('.index_follow_button').html('<%=  escape_javascript(render :partial => 'partials/index_follow_button.html.erb', :locals => {:artist => @artist}) %>');
    

    在您的 index.html.erb 中,您的循环设置局部变量 artist


    看起来您还需要一种独特的方式让 jquery 来处理有问题的元素。

    <% @artists.each do |artist| %>
      <div class="index_follow_button" id="artist-<%= artist.id %>">
        <%= render "partials/index_follow_button", :artist => artist %>
      </div>
    <% end %>
    

    然后再次更改follow_button.js.erb;这次使用新的 id 作为 css 定位器而不是类。

    $('#artist-<%= @artist.id %>').html('<%=  escape_javascript(render 
      :partial => 'partials/index_follow_button.html.erb', 
      :locals => {:artist => artist}) %>'
    );
    

    【讨论】:

    • 这不起作用,因为单击follow 按钮会导致index.html.erb 页面上的每个follow 按钮都转换为unfollow
    • 他们都有相同的.index_follow_button html 类吗?我怀疑您的 jquery 调用正在用该类覆盖每个元素。
    • 啊,你是对的!什么是只有被点击改变的正确方法?
    • 你需要一些独特的东西;然后你可以设置元素的 id 并在 jquery 中使用它。艺术家是独一无二的吗?或者你可以使用artist.id吗?
    【解决方案2】:

    我认为您错过了 follow_button.js.erb 文件中的 @artist 变量。

    $('.index_follow_button').html('<%= escape_javascript(render :partial => 'partials/index_follow_button.html.erb', :locals => {:artist => @artist}) %>');

    【讨论】:

    • 不错的 Saiqul,比我快 12 秒!
    • 这不起作用,因为单击follow 按钮会导致index.html.erb 页面上的每个follow 按钮都转换为unfollow
    • 其实我20分钟前就想回答这个问题,但是我很忙,哈哈
    • 它是 @artists = Artist.all,但是在 relationships_controller 中调用 Ajax 之后,不是所有东西都从那里使用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 2016-11-12
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    相关资源
    最近更新 更多