1

当我更改选择时,我在更新 div 时遇到了一些问题。

首先,我在这样的视图中创建我的选择。

=collection_select(:department, :id, Department.all, :id, :name, options={:prompt=>"Seleccione departamento"})

现在我创建一个 ajax 调用来获取 select 的值并找到所有要匹配的日期

:javascript
  $(document).ready(function(){
  $('#department_id').change(function(){
    $.ajax({
      url:"/movements/find_by_department",
      type: "GET",
      data: {valor: $('#department_id').val()}
    });
  });
  })

下一步是创建一个方法“find_by_department”来获取值..并显示一些东西

 def find_by_department
    id_department = (params[:valor])
    @ideas_department = Idea.find_by_department_id(id_department)

    respond_to do |format|
      format.js {render :department_chart}
    end
  end

现在是我的问题,我有一个部分的“department_chart”

$("#my_id4").html("@ideas_department")

但 id="my_id4" 不会改变

另一方面,我在控制台中看到了这个

Started GET "/movements/find_by_department?valor=5" for 127.0.0.1 at 2013-08-22 09:50:04 +0100
Processing by MovementsController#show as */*
  Parameters: {"valor"=>"5", "id"=>"find_by_department"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 10 LIMIT 1
  Option Load (0.2ms)  SELECT "options".* FROM "options" WHERE "options"."name" = 'ideas_status_updated_date' LIMIT 1

我不知道为什么要调用 show 方法

我的路由器里有这个

  get 'movements/find_by_department', :as => 'find_by_department'
4

1 回答 1

1

像这样改变你的路线:

resources :movements do
   get 'find_by_department', on: :member      
end

将您的 javascript 代码更改为:

 :javascript
  $(document).ready(function(){
  $('#department_id').change(function(){
    $.ajax({
      url:"/movements/#{$('#department_id').val()}/find_by_department",
      type: "GET"
    });
  });
  })

在您的控制器中:

def find_by_department
 @ideas_department = Idea.find_by_department_id(params[:id])

 respond_to do |format|
  format.js {render :department_chart}
 end

结尾

如果您想根据来自请求的 id 更改您的 id,请将您的代码更改为:

$("#my_id#{@ideas_department.id }").html("<%= @ideas_department %>")

希望它会有所帮助。谢谢

于 2013-08-22T09:12:52.000 回答