input in nested form is not shown in rails

ekremkaraca

I have a Rails app that has two models: Question and possible answer. A question has many possible answers and a possible answer belongs to a question. While adding question, I'd like to add possible answers. However, I can't make possible answers input shown. Here are files may cause the problem:

questions_controller.rb:

  def new
    @question = @poll.questions.build
    4.times { @question.possible_answers.build }
  end

question.rb:

has_many :possible_answers

accepts_nested_attributes_for :possible_answers

_form.html.haml:

-f.fields_for :possible_answers do |p|
  =p.text_field :title

How can I fix the issue?

x6iae

Haa... I see that. Took me a while though.

In erb, the difference between <% ... %> and <%= ... %> is that the former only analyses, while the later shows the result as well.

The same principle applies when other engines are being used as well (slim, haml, etc).

To analyse, use -, but to display as well, use =.

So, to apply the above to your case, the line for the nested attributes has to be changed from only analysing, to also displaying.

That is: change this:

-f.fields_for :possible_answers do |p|
  =p.text_field :title

to this:

=f.fields_for :possible_answers do |p|
  =p.text_field :title

and that should do it for you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related