1

I am using rails cast 196 ( http://railscasts.com/episodes/196-nested-model-form-revised?autoplay=true ) to help me on my project, I am trying to modify the code so that when I make a new question, I am able to change the survey's title in the form. right now the form submits but no change is made to the Survey's title in the db

class Survey < ActiveRecord::Base
  has_many :questions
end

I moved the accepts_nested_attributes_for from the Survey to the Question model

class Question < ActiveRecord::Base
  belongs_to :survey
  accepts_nested_attributes_for :survey
end

I added the :title (the field that I want to modify) in the questions pramas

def create
    @question = Question.new(question_pramas)
end

def question_pramas
    params.require(:question).permit(:content, survey_attribute: :title)
end

View

<%= form_for(@question) do |f| %>
  <%= f.fields_for :surveys do |builder| %>
    <%= builder.label :title %>
    <%= builder.text_field :title, class: 'form-control' %>
  <% end %>
  ...
1
  • Is the problem happens while creating or updating? Commented Oct 4, 2015 at 16:24

2 Answers 2

2

It looks like you need to change your question_prams method to specify "survey_attributes".

def question_pramas
  params.require(:question).permit(:content, survey_attributes: [:title])
end
Sign up to request clarification or add additional context in comments.

1 Comment

Could you share the post / put parameters from the server log?
1

To further Tom Fast's answer, you'll also need to get your associative names sorted properly:

<%= f.fields_for :survey do |builder| %>
    <%= builder.label :title %>
    <%= builder.text_field :title, class: 'form-control' %>
  <% end %>

#app/controllers/questions_controller.rb
class QuestionsController < ApplicationController
   def question_params
      params.require(:question).permit(:content, survey_attributes: [:title])
   end
end

1 Comment

the text field is not visible anymore when i change :surveys to :survey

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.