I posted today similar questions two times

我今天发了两次类似的问题

how-to-use-local-or-instance-variable-in-ruby-code-in-coffeescript-in-haml-templ

how-to-use-local-or-instance-variable-in-ruby-code-in-coffeescript-in-haml-templ

ajax-in-rails-returns-alway-error-callback

ajax-in-rails-returns-alway-error-callback

I'm confusing about these issues please let me ask.

我对这些问题感到困惑,请让我问一下。

I'm using ajax in haml to reload a part of page with data from controller.

我在haml中使用ajax重新加载来自controller的数据。

Please see my code.

请参阅我的代码。

.html.haml

.html.haml

= render 'layouts/partial' #This is partial. I want to reload this part

:coffee
    $('input#field').change ->
        $.ajax
            url: '/posts/gaga'
            type: "POST"
            dataType: 'text'
            error: (jqXHR, textStatus, errorThrown) ->
                alert "error"
            success: (data, textStatus, jqXHR) ->
                alert "success"
                $('div.kore').html('#{ j( render( 'layouts/partial' ) )} ');

posts_controller.rb

posts_controller.rb

def gaga
    @model = Model.new("blabla")
    render :text => @model
end

_partial.html.haml

_partial.html.haml

- if @model
    = @model.html_safe
- else
    = "No data"

First, I thought data can be get through @model instance variable from controller but it couldn't.

首先,我认为数据可以通过控制器的@model实例变量获得,但它不能。

Second, I tryed to get data from ajax response data to view(_partial.html.haml) with jQuery.

其次,我尝试使用jQuery从ajax响应数据获取数据到view(_partial.html.haml)。

Callback and response return correctly as text.

回调和响应以文本形式正确返回。

How do I pass data to view from ajax response data, or is there other ways?

如何从ajax响应数据传递数据到视图,还是有其他方法?

Sorry for my sloppy English. I hope someone give me the clue.

对不起,我的英语太马虎了。我希望有人能给我一些线索。

Thanks in advance!

提前谢谢!

1 个解决方案

#1


2

You can set the ajax request to /posts/gaga.json and in your controller's gaga method:

可以将ajax请求设置为/posts/gaga。json和你的控制器的gaga方法:

respond_to do |format|
  format.html
  format.json { render :json => @model }
end

You may want to prepare gaga.html.haml view file if you intend to also access /posts/gaga not via ajax, or just remove format.html if you only intend to respond in json.

您可能需要准备gaga.html。haml视图文件如果你也想访问/post /gaga而不是通过ajax,或者只是删除格式。如果您只想用json响应,请使用html。


To clarify, I see you want to reload the partial after ajax request. So rather than:

为了澄清,我看到您希望在ajax请求后重新加载部分。而不是:

$('div.kore').html('#{ j( render( 'layouts/partial' ) )} ');

you can do something like this:

你可以这样做:

# inside gaga methods
format.json do
  render(json: {
    view: render_to_string(partial: 'layouts/partial', layout: false)
  })
end

Now inside your view:

现在在你的观点:

= render 'layouts/partial' #This is partial. I want to reload this part

:coffee
  $('input#field').change ->
    $.ajax
      url: '/posts/gaga.json'
      type: "POST"
      error: (jqXHR, textStatus, errorThrown) ->
        alert "error"
      success: (data, textStatus, jqXHR) ->
        alert "success"
        $('div.kore').html(data.view)

更多相关文章

  1. 如果外部应用程序更改了持久模型(服务器数据库),AngularJS可以自
  2. Javascript来自数据库的图像上的图像映射
  3. python数据类型二(列表和元组)
  4. 将敏感数据存储在数据库中,建议
  5. sklearn进行数据预处理-归一化/标准化/正则化
  6. 使用特定顺序的ID列表从Django数据库中获取记录
  7. 检查 NaN 数据值 (C/C++/Python 实现)
  8. django的视图和URL配置
  9. 【python网络爬虫三】爬取动态数据及数据入库

随机推荐

  1. Android 无线接口层RIL
  2. Android 基于注解IOC组件化/模块化的架构
  3. android中AVD的使用
  4. Android实践手册:该如何应用Android知识到
  5. Ubuntu14.04下最新Android(安卓)NDK安装
  6. Android(安卓)中 Bitmap 和 Drawable 相
  7. 安卓开发一步到位
  8. 读取Android联系人列表
  9. 独立android开发者开发app如何盈利
  10. android安卓手机分屏多窗口实现方法