Rails AJAX - getting a dropdown selection to populate a table

后端 未结 3 720
醉话见心
醉话见心 2020-12-17 07:38

I\'m trying to get a selection from a dropdown to populate a table in the same page, using AJAX so that changes appear without a page refresh. I\'ve got as far as working ou

3条回答
  •  醉酒成梦
    2020-12-17 07:53

    Assume you're using jquery

    <%= collection_select :project, :id, Project.all, :id, :name, {}, {:onchange => '$.get("/populate_projects")'} %>
    

    then in your controller

    def populate_projects
      @project = Project.find(params[:id])
      respond_to do |format|
        ...
        format.js { render 'populate_projects', :formats => [:js] }
        ...
      end
    end
    

    finally, create populate_projects.js.erb and write any change table content script. @project is available in that script.

提交回复
热议问题