简体   繁体   English

通过AJAX用刷新的数据渲染部分图像

[英]Render partial with refreshed data via AJAX

I am pretty new to JS, but I hope I am missing something simple. 我对JS很陌生,但是我希望我缺少一些简单的东西。 I am trying to reload a partial that displays an image asynchronously. 我正在尝试重新加载异步显示图像的部分图像。 When a user clicks on the link below, it registers their vote, then (it should) render the pins partial with a new image from the database. 当用户单击下面的链接时,它会注册他们的投票,然后(应该)使用数据库中的新图像来部分渲染图钉。

index.html.erb (The link) index.html.erb(链接)

<%= link_to 'Less', {:controller => "pins", 
                     :action => "create_view", 
                     :rank => 0,
                     :user_id => current_user.id, 
                     :pin_id => @pins.id
                     },
                     :method => "post", :remote => true %>

pins_controller.rb (Method called by link) pins_controller.rb(链接调用的方法)

def create_view
  @view = View.new
  @view.user_id = params[:user_id]
  @view.pin_id = params[:pin_id]
  @view.rank = params[:rank]

  if @view.save
   respond_to do |format|
    format.html { redirect_to root_path }
    format.js
   end
  else
   render action: "new"
  end
end

pins_controller.rb (Method that generates @pins) pins_controller.rb(生成@pins的方法)

def index
  seen = Array.new
  max_pins = 20
  sex = current_user.sex
  views = View.user_views(current_user)
  seen = views.map(&:pin_id)
  @pins = Pin.new_pin(seen, sex)

models/pins.rb 车型/ pins.rb

def self.new_pin(pin_ids, sex)
  b = TRUE
  self.find(:first, :conditions => ["sex = ? AND active = ? AND id not in (?)", sex, b,    pin_ids], :order => "created_at desc" )
end

index.html.erb (The DIV and partial) index.html.erb(DIV和部分)

<div id="main_well">
     <%= render @pins %>
</div>

create_views.js.erb (The javascript triggered on click) create_views.js.erb(点击时触发的javascript)

$('#main_well').html("<%= escape_javascript(render (partial: "pin", :locals => { pin: @pins})) %>");

_pin.html.erb (The partial to be reloaded by JS) _pin.html.erb(要由JS重新加载的部分)

<div class="row">
 <div class="center_well">
   <div class="well" style="background-image: url('<%= pin.image %>')" onclick="window.open('<%= pin.product_url %>','mywindow');"></div>
 </div>
</div>

I have been at this for a while. 我已经有一段时间了。 Here's what I know. 这就是我所知道的。 Clicking the link does remotely update the database. 单击链接不会远程更新数据库。 And if I change the JS to $('#main_well').remove(), clicking the link does remove the image asynchronously. 如果我将JS更改为$('#main_well')。remove(),则单击链接确实会异步删除图像。 However, I cannot for the life of my get the image to update onclick without refreshing the page. 但是,我一生都无法在不刷新页面的情况下更新onclick图片。 Thanks in advance for your help. 在此先感谢您的帮助。

EDIT 编辑

As in the answer below, the instance variable @pins was not leading in the create_views method. 如下面的答案所示,实例变量@pins不在create_views方法中。 Thus, regenerating the instance variable link this seemed to solve the problem. 因此,重新生成实例变量链接似乎可以解决问题。

pins_controller.rb pins_controller.rb

def create
 seen = Array.new
 max_pins = 20
 sex = current_user.sex
 views = View.user_views(current_user)
 seen = views.map(&:pin_id)
 @pins = Pin.new_pin(seen, sex)

 @view = View.new
 @view.user_id = params[:user_id]
 @view.pin_id = params[:pin_id]
 @view.rank = params[:rank]

 if @view.save
  respond_to do |format|
     format.html { redirect_to root_path }
     format.js
  end
 else
  render action: "new"
 end
end

However, this solution only serves to reload the partial successfully once. 但是,此解决方案仅能成功重新加载部分。 If I click the link, it reloads the image. 如果单击链接,它将重新加载图像。 However, if I click the link again, nothing happens. 但是,如果我再次单击该链接,则什么也没有发生。 However, if I change the function like so it works perfectly: 但是,如果我像这样更改函数,它就可以完美地工作:

pins_controller.rb pins_controller.rb

def create
 seen = Array.new
 max_pins = 20
 offset = rand(Pin.count)
 @pins = Pin.first(:offset => offset)

 @view = View.new
 @view.user_id = params[:user_id]
 @view.pin_id = params[:pin_id]
 @view.rank = params[:rank]

 if @view.save
  respond_to do |format|
     format.html { redirect_to root_path }
     format.js
  end
 else
  render action: "new"
 end
end

It seems that the local variable "view" is not being reloaded the second time I click the link. 似乎我第二次单击链接时未重新加载局部变量“ view”。 I have no idea why. 我不知道为什么。 Any help would be greatly appreciated. 任何帮助将不胜感激。

views.rb views.rb

def self.user_views(user)
  self.find(:all, :conditions => ['user_id = ?', user])
end

Update 更新

In reference to the comments, this worked: 在评论中,这是可行的:

def index
  seen = Array.new
  max_pins = 20
  sex = current_user.sex
  views = View.user_views(current_user)
  seen = views.map(&:pin_id)
  @pins = Pin.new_pin(seen, sex)
end

def create
  seen = Array.new
  max_pins = 20
  sex = current_user.sex
  views = View.user_views(current_user)
  seen = views.map(&:pin_id)
  @pins = Pin.new_pin(seen, sex)

  @view = View.new
  @view.user_id = params[:user_id]
  @view.pin_id = params[:pin_id]
  @view.rank = params[:rank]

  if @view.save
     respond_to do |format|
         format.html { redirect_to root_path }
         format.js
     end
  else
     render action: "new"
  end
end

Original Answer 原始答案

From looking at your code, it seems it should work, but I imagine there are syntax problems preventing it from doing so 通过查看您的代码,它似乎应该可以工作,但是我认为存在语法问题,导致代码无法正常工作


@pins @pins

You're calling @pins in a completely new action 您正在以全新的方式调用@pins

The problem is that your ajax request is a new request (your instance is concluded after the initial HTTP request ends). 问题在于您的ajax请求是一个新请求(您的实例在初始HTTP请求结束后就结束了)。 This means your @pins instance variable is not persisting through the ajax call, and is likely why your partial won't render correctly 这意味着您的@pins实例变量无法通过ajax调用持久化,这很可能是您的部分变量无法正确呈现的原因

Try: 尝试:

#app/controllers/pins_controller.rb
def index
    @pins = Pin.new_pin(seen, sex)
end

def create
    @pins = Pin.find(params[:id])
end

Partial 局部

When calling your partial, you can then try this: 致电您的分公司时,您可以尝试以下操作:

$('#main_well').html("<%=j render partial: "pin", :locals => { pin: @pins} %>");

This should work if your @pins variable is called properly 如果您的@pins变量被正确调用,这应该可以工作


Update 更新

def self.user_views(user)
   where(user_id: user.id)
end

Try changing this: 尝试更改此:

$('#main_well').html("<%= escape_javascript(render (partial: "pin", :locals => { pin: @pins})) %>");

to this (add html_safe to the end of the escape_javascript call): 为此(将html_safe添加到escape_javascript调用的末尾):

$('#main_well').html("<%= escape_javascript(render :partial => 'pin', :locals => { pin: @pins}).html_safe %>");

EDIT 编辑

The problem is probably related to how the partial is being rendered. 问题可能与局部渲染的方式有关。 Does this work? 这样行吗?

$('#main_well').html("<%= escape_javascript("#{image_tag pin.image}").html_safe %>")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM