简体   繁体   English

Rails Ajax请求不会刷新数据

[英]rails ajax request doesn't refresh data

i have some troubles with my ajax request in a rails app. 我在Rails应用程序中对我的Ajax请求有一些麻烦。

i make a request, i take some params from a link when i click, and then i want to refresh my partial that contains images from my database. 我提出一个请求,当我单击时从链接中获取一些参数,然后我想刷新包含数据库中图像的部分。

here is my ajax 这是我的ajax

    $.ajax({

        url:"gallery",
        data: { pie :categoria},
        type: "GET",
        async: true,
        dataType: "script"


    });

the thing is that in the rails shell i get passed on my params. 事实是,在rails shell中,我通过了参数。 but it doesnt make any changes to the view. 但它不会对视图进行任何更改。


my shell: 我的壳:

Started GET "/gallery?pie=VibPellizcables&_=1375047201412" for 127.0.0.1 at Sun Jul 28 18:33:21 -0300 2013
Processing by GalleryController#pro as JS
  Parameters: {"pie"=>"VibPellizcables", "_"=>"1375047201412"}
  Product Load (0.1ms)  SELECT `products`.* FROM `products` WHERE `products`.`pie` = 'VibPellizcables'
  Rendered sexytoys/_algo.html.haml (0.1ms)
Completed 200 OK in 1ms (Views: 0.6ms | ActiveRecord: 0.1ms)

i want to update a partial which contains images from my database. 我想更新一个包含来自数据库的图像的局部图像。

this is the partial i want to update. 这是我要更新的部分内容。

  #miniDiv 

    %ul#miniRecuadro
      - @products.each do |product|
        %li.mini= image_tag product.pic.url(:thumb)

my controller: 我的控制器:

      def gallery
        @products = Product.find(:all, :conditions => {:pie => params[:pie]})
        #render :partial => "algo"
        flash[:notice] = "Mensaje ENVIADO >>"
        respond_to do |format|

          format.js {render :partial => 'algo', :conditions => {:pie => params[:pie]}}
         # format.json { render :json => @products  }
        end

You have to do that replacement either in response or AJAX callback: 您必须在响应或AJAX回调中进行该替换:

Response doing replacement 更换响应

You need to render a JS file: 您需要呈现一个JS文件:

# app/controllers/gallery_controller.rb
def pro
  ...
  respond_to do |format|
    format.js # no render here
  end
  ...
end

# app/views/gallery/pro.js.erb
$('#your-container').html('<%= j( render partial: 'algo' )%>')

I am not sure about this path, application will complain if this path is wrong, you will see this in browser console as this is AJAX. 我不确定此路径,如果此路径错误,应用程序将抱怨,您将在浏览器控制台中看到此内容,因为这是AJAX。 I don't know about conditions option is supported in view either. 我也不知道在视图中是否支持conditions选项。

Callback doing replacement 回调进行替换

$.ajax({

    url:"gallery",
    data: { pie :categoria},
    type: "GET",
    async: true,
    success: function(response){
      $('#your-container').html(response);
    }


});

Note I removed dataType: 'script' here, since you don't want to execute server response. 注意我在这里删除了dataType: 'script' ,因为您不想执行服务器响应。

In this case, you could respond with that html you are responding: 在这种情况下,您可以使用您正在响应的html进行响应:

# gallery_controller.rb
...
  format.html {render :partial => 'algo', :conditions => {:pie => params[:pie]}}
...

Note I changed format of your response since you are rendering html. 注意由于您正在渲染html,因此我更改了响应的格式。

Shortcut 捷径

Lastly, if you want to load content from your server inside a container in your page you could be interested in load : 最后,如果要从服务器在页面的容器内加载内容,您可能会对load感兴趣:

$('#your-container').load('gallery', { pie: categoria });

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

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