【发布时间】: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 => 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