Giter Site home page Giter Site logo

railscast-surveysays's Introduction

README

Note: I got my personal project working after I figured out how to get the remove to work. I didn’t finish going through the railscast tutorial and submitting this code.

I recommend using this guide instead of mine: github.com/dnewkerk/nested-model-form . Just note to use <fieldset> !!

Getting Nested Forms to Work

app/controller/surveys_controller.rb
def new
  @survey = Survey.new
  3.times do
    question = @survey.questions.build 
    4.times { question.answers.build }
  end
end
def survey_params
  params.require(:survey).permit(
  :name,
  questions_attributes: [:id, :survey_id, :content, :_destroy,
   answers_attributes: [:id, :question_id, :content, :_destroy]])
end

Getting Remove to Work

app/views/surveys/_answer_fields.html.erb
<div class="field">
    <%= f.label :content, "Answers" %>
    <%= f.text_field :content %>
    <%= f.hidden_field :_destroy %>
    <%= link_to "remove", "#", class: "remove_fields" %>
</div>

you can’t use a div! Use fieldset instead

<fieldset>
    <%= f.label :content, "Answers" %>
    <%= f.text_field :content %>
    <%= f.hidden_field :_destroy %>
    <%= link_to "remove", "#", class: "remove_fields" %>
</fieldset>
app/assets/javascript/survey.js.coffee
ready = ->
  $('form').on 'click', '.remove_fields', (event) ->
    $(this).prev('input[type=hidden]').val('1')
    $(this).closest('fieldset').hide()
    event.preventDefault()

  $('form').on 'click', '.add_fields', (event) ->
    time = new Date().getTime()
    regexp = new RegExp($(this).data('id'), 'g')
    $(this).before($(this).data('fields').replace(regexp, time))
    event.preventDefault()

$(document).ready(ready)
$(document).on('page:load', ready)

railscast-surveysays's People

Contributors

falieson avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.