在coffeescript中,我如何在我的控制器中创建一个变量并通过ajax返回结果来填充一个数据表?

时间:2022-12-01 16:12:54

So I have a view with a datatable (javascript library to create pretty tables) that is generated like in this RailsCast Briefly, the datatable is created in the server side and a new class creates a json response that is rendered in the view

因此,我有一个datatable (javascript库创建漂亮的表)的视图,它在这个RailsCast中生成,它是在服务器端创建的,一个新类创建一个在视图中呈现的json响应。

So in my generated view I have a link that I want to trigger an ajax event when clicked and has a data attribute,

在我生成的视图中,我有一个链接,我想在点击并有一个数据属性时触发一个ajax事件,

 link_to( "#{sir.sir_id}" , '#', :data => {'sir-id' => sir.id}, remote: true )

I fetch the value of that data attribute in coffeescript this way:

我这样在coffeescript中获取数据属性的值:

  $("a[data-sir-id]").click ->
   data_sir_id = $(this).data("sir-id")

which works fine

工作的好

I want to make that value (sir-id) available in my controller so I can get the associated model objects and show them in the same view via ajax, it would fill the content of another datatable (this one would not need server-side processing)

我想让这个值(sir-id)在我的控制器中可用,这样我就可以获得关联的模型对象并通过ajax在同一个视图中显示它们,它将填充另一个datatable的内容(这个不需要服务器端处理)

How do I create and feed this new datatable with ajax source?

如何使用ajax源创建和提供这个新的datatable ?

I think I could return some other json object to the view if I manage to use sir_id in my controller but the view has already rendered json when first created.

如果我在控制器中使用sir_id,我想我可以向视图返回一些其他的json对象,但是视图在第一次创建时已经呈现了json。

1 个解决方案

#1


1  

You can send the data to the controller using a post method.

可以使用post方法将数据发送给控制器。

$("a[data-sir-id]").click ->
  data_sir_id = $(this).data("sir-id")
  $.ajax
    type: 'POST'
    url: '/some-path'
    data: {sir_id: data_sir_id}
    success: console.log('success posting sir-id')
    dataType: 'json'

#config/routes.rb
Rails.application.routes.draw do
  post "/some-path", to: "some_controller#some_view"
end

#app/controllers/some_controller.rb
class SomeController < ApplicationController
  def some_view
    SomeModel.find_by_sir_id(params[:sir_id])
  end
end

I would like to say that if you can form a link_to with "#{sir.sir_id}" in your view file it seems to me that the sir_id should already be available to you in your controller and you wouldn't need to use ajax. Nevertheless, I hope this ajax solution is what you were looking for :)

我想说的是,如果您能与“#{先生”形成一个link_to。在您的视图文件中,在我看来,在您的控制器中应该已经有了sir_id,您不需要使用ajax。不过,我希望这个ajax解决方案正是您所需要的:)

#1


1  

You can send the data to the controller using a post method.

可以使用post方法将数据发送给控制器。

$("a[data-sir-id]").click ->
  data_sir_id = $(this).data("sir-id")
  $.ajax
    type: 'POST'
    url: '/some-path'
    data: {sir_id: data_sir_id}
    success: console.log('success posting sir-id')
    dataType: 'json'

#config/routes.rb
Rails.application.routes.draw do
  post "/some-path", to: "some_controller#some_view"
end

#app/controllers/some_controller.rb
class SomeController < ApplicationController
  def some_view
    SomeModel.find_by_sir_id(params[:sir_id])
  end
end

I would like to say that if you can form a link_to with "#{sir.sir_id}" in your view file it seems to me that the sir_id should already be available to you in your controller and you wouldn't need to use ajax. Nevertheless, I hope this ajax solution is what you were looking for :)

我想说的是,如果您能与“#{先生”形成一个link_to。在您的视图文件中,在我看来,在您的控制器中应该已经有了sir_id,您不需要使用ajax。不过,我希望这个ajax解决方案正是您所需要的:)