开发者

Dynamically adding elements to a form

开发者 https://www.devze.com 2023-04-05 05:11 出处:网络
My goal is to have a form where I fill in some details and then I can hit \'add question\' which is a link that should dynamically add a question.

My goal is to have a form where I fill in some details and then I can hit 'add question' which is a link that should dynamically add a question.

i.e.


Date:

Name:

Description:

Add a Question

Create


my _form.html.haml starts off as such as such (this form is under views/interviews since my goal is to be able to add questions to an interview. However, questions can be used independently of interviews as well)

= form_for ([:posting, @interview]) do |ff|<br>
     - if @interview.errors.any?

   . . . (more fields) . . .

.field{:id => "overall_questions"} 
    =link_to "Add a Question", new_question_path, :remote => true
    %br/
    %br/

.field{:id开发者_如何学编程 => "individual_questions"} 
    =ff.label :whyyy
    =ff.fields_for  :questions, @interview.questions do |builder|
        = render :partial => "question_fields" 
    %br/  
    %br/

.actions
    = ff.submit 

_question_fields.html.haml

.fields{:id => "question#{builder.object.id}"}
    =ff.label :content, "Question"
    =link_to "Remove", question_path(builder.object.id), :remote => true, :confirm => "Are you sure?", :method => :delete
    %br/
    =ff.text_area :content, :rows => 3

the :remote => true (from _form.html.haml) gets directed to my questions_controller.rb

def new

    @question = Question.new
    respond_to :js

end

this links to my new.js.erb file under views/questions

$('#individual_questions').clone().appendTo($('#overall_questions'))

The thing is, I'm not quite sure how to call the _question_fields.html.haml. I know the js.erb file works because I've been able to run alert boxes when clicking 'Add a question' and it also currently appends the Whyyy from the field with id individual questions.


One way to do this is outlined in the following episode of RailsCasts:

http://railscasts.com/episodes/74-complex-forms-part-2

As far as the .haml question, you should be able to modify the example in the episode to figure that part out. So long as it's name follows the convention (and on quick glance it looks like it does), it should work without too much trouble.

0

精彩评论

暂无评论...
验证码 换一张
取 消