Adding dynamic fields to nested form through AJAX

前端 未结 2 725
野趣味
野趣味 2021-02-06 01:39

I\'ve been watching and reproducing these railscasts on my app: 196-nested-model-form-part-1 and 197-nested-model-form-part-2. I do not yet have a pro account so i can\'t watch

2条回答
  •  不要未来只要你来
    2021-02-06 02:05

    Did you know about remote links and forms? You can use a remote link here to accomplish what you want.

    In your view:

    link_to 'Add question', add_question_path(@survey), remote: true
    

    In your controller

    def add_question
      @survey = Survey.find(params[:id])
    
      respond_to do |format|
        format.js #add_question.js.erb
      end
    end
    

    The last step is to create a file app/views/surveys/add_question.js.erb

    $('#my_survey').append('<%=j render partial: 'my_question_partial' %>')
    

    Don't forget to create a route for your ask_question_path

    For more info about remote links and forms see: http://tech.thereq.com/post/17243732577/rails-3-using-link-to-remote-true-with-jquery-ujs

提交回复
热议问题