ajax in rails with collection_select

2019-08-28 23:01发布

问题:

H everyone I am try to refresh this div #my_id4 with the request of the consult that I execute when change a collection_select with ajax.

I have in my view the select with javascript like this...

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

this is the javascript code

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

in my router I have this...

resources :movements do
  get 'find_by_department'
end

and in my controller I have this

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

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

in the view department_chart.js.erb I have this.

$("#my_id4").html("change!!!")

but when I execute the application I obtain this..

Request URL:http://localhost:3000/movements/find_by_department?valor=5
Request Method:GET
Status Code:302 Found
Request Headersview source

and the responce in empty! is like don't execute the method.

Any idea!!!

the log is this

Started GET "/movements/find_by_department?valor=6" for 127.0.0.1 at 2013-08-23 14:46:28 +0100
Processing by MovementsController#show as */*
  Parameters: {"valor"=>"6", "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
Se ha consultado permiso para admin::movements::show
Redirected to http://localhost:3000/movements
Filter chain halted as :permission_check rendered or redirected
Completed 302 Found in 4ms (ActiveRecord: 0.5ms)


Started GET "/movements" for 127.0.0.1 at 2013-08-23 14:46:28 +0100
Processing by MovementsController#index as */*
  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
Se ha consultado permiso para admin::movements::index
  Company Load (0.2ms)  SELECT "companies".* FROM "companies" WHERE "companies"."id" = 4 LIMIT 1
  Movement Load (0.5ms)  SELECT "movements".* FROM "movements" INNER JOIN "ideas" ON "movements"."idea_id" = "ideas"."id" INNER JOIN "departments" ON "ideas"."department_id" = "departments"."id" WHERE "departments"."company_id" = 4
  Department Load (0.3ms)  SELECT "departments".* FROM "departments" 
  Rendered movements/index.html.haml within layouts/logged (5.2ms)

回答1:

In the log:

Se ha consultado permiso para admin::movements::show
Redirected to http://localhost:3000/movements
Filter chain halted as :permission_check rendered or redirected

There looks to be a permission failure for the user making the request that is causing the redirection.