0

I am quite new to both programming and ruby on rails. I have followed http://ruby.railstutorial.org/ and then i have started to watch episodes from http://railscasts.com. What i am trying to do is that "Handling multiple models in a single form". Below you will see my models and their assosications and also the view of form that i am trying to get info from users.

My modelling is that;

There are employers, employers have interviews and interviews have questions.

Customquestion model:

class Customquestion < ActiveRecord::Base
  attr_accessible :content
  belongs_to :interview

  validates :content, length: {maximum: 300}
  validates :interview_id, presence: true
end

Interview model:

class Interview < ActiveRecord::Base
  attr_accessible :title, :welcome_message
  belongs_to :employer
  has_many :customquestions, dependent: :destroy
  accepts_nested_attributes_for :customquestions

  validates :title, presence: true, length: { maximum: 150 }
  validates :welcome_message, presence: true, length: { maximum: 600 }
  validates :employer_id, presence: true
  default_scope order: 'interviews.created_at DESC'
end

Form to create new interview;

<%= provide(:title, 'Create a new interview') %>
<h1>Create New Interview</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(@interview) do |f| %>
    <%= render 'shared/error_messages_interviews' %>

      <%= f.label :title, "Tıtle for Interview" %>
      <%= f.text_field :title %>

      <%= f.label :welcome_message, "Welcome Message for Candidates" %>
      <%= f.text_area :welcome_message, rows: 3 %>

      <%= f.fields_for :customquestions do |builder| %>
        <%= builder.label :content, "Question" %><br />
        <%= builder.text_area :content, :rows => 3 %>
      <% end %>
      <%= f.submit "Create Interview", class: "btn btn-large btn-primary" %>
    <% end %>
  </div>
</div>

When i fill the form with required information and submit it, i get following error;

Can't mass-assign protected attributes: customquestions_attributes

Application Trace | Framework Trace | Full Trace
app/controllers/interviews_controller.rb:5:in `create'
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"cJuBNzehDbb5A1Zb14BjBfz1eOsjBCDzGhYKT7q6A0k=",
 "interview"=>{"title"=>"",
 "welcome_message"=>"",
 "customquestions_attributes"=>{"0"=>{"content"=>""}}},
 "commit"=>"Create Interview"}

I hope that i have provided enough information for you guys to understand what is problem with that case.

Thank you in advance

hayri
  • 5
  • 1

1 Answers1

2

Just follow what is written in the error message: try to add attr_accessible :customquestions_attributes to Interview model:

class Interview < ActiveRecord::Base
   attr_accessible :title, :welcome_message, :customquestions_attributes
...
Mik
  • 4,117
  • 1
  • 25
  • 32
  • Solved the problem thank you, but now new problem occured. New problem isnt related to that but maybe you might have an idea about it. Create action in InterviewController doesnt get the id of interview and put it database table of customquestions. My current create action for interviews controller is as follows and i know that i should add something to it : ` def create @interview = current_employer.interviews.build(params[:interview]) if @interview.save flash[:success] = "Interview created!" redirect_to @interview else render 'new' end end ` @mikhail-d – hayri Jun 09 '12 at 20:53
  • @hayri, I'm sorry, but I can not find the error here. Maybe I did not understand the question... If you're stuck with it - feel free to ask a new question. – Mik Jun 09 '12 at 21:04
  • i am really stucked with that, now when i submit the form, although i fill the all fields, it raises a question saying that "Customquestions interview can't be blank". And i am sure it is because of some missing code in Interview controller. As i posted in last comment, you can see my create action in Interview Controller, and i feel like that i should add something to that create action so that database table of customquestions will have access to interview_id. Right now create action doesnt get the interview_id and it raises that error "Customquestions interview can't be blank". – hayri Jun 09 '12 at 21:16