0

I have found a lot of posts about this Railscast but all the suggestions haven't helped me. I have been able to render in the view a nested form field, but only one, not the 3 that I have called out in my controller. When I submit, I get the error: Can't mass-assign protected attributes: clue

Chapter.rb

class Chapter < ActiveRecord::Base
 belongs_to :trail
 has_many :clues, :dependent => :destroy
 accepts_nested_attributes_for :clues
 attr_accessible :asset, :assetkind, :description, :gate, :name, :trail, :trail_id, :cover
.
.
.

end

Clue.rb

class Clue < ActiveRecord::Base
 attr_accessible :chapter_id, :theclue, :typeof, :chapter
 .
 .
 .
 belongs_to :chapter
end

In the railcast it says to use the equivalent of :clues, and this renders 3 fields. But in mine, it didn't render the fields. Instead, I use @chapter.clues and it renders only one.

My form when making a new chapter.

<h1>Add a New Chapter</h1>
<h3>Add To Trail : <%= @trail.title %></h3><br>
<%= form_for [@trail, @trail.chapters.build] do |f| %>

<h6>About the Chapter</h6>
    <%= f.label :name, 'Chapter Name' %>
    .
    .
    .
<h6>Progressing the Story</h6>
    <%= f.fields_for @chapter.clues do |builder| %>
    <p>
        <%= builder.label :theclue, "Enter Clue" %>
        <%= builder.text_area :theclue, :rows => 2 %>
    </p>
<% end %>
     .
     .
     .
<% end %>

My chapters_controller.rb new

class ChaptersController < ApplicationController

def new
  @trail = Trail.find(params[:trail_id])
  @chapter = Chapter.new
  @title = "Chapter"
  3.times { @chapter.clues.build }
  logger.debug "CHAPTER!!!!!!!!!!!!new: am i in a trail? #{@trail.to_yaml}"
  logger.debug "CHAPTER!!!!!!!!!!!!new: am i in a clue? #{@chapter.clues.to_yaml}"
end

My log shows me 3 clues, but the attributes are empty (no :id). Is this a sign of something wrong? So even though my log shows 3 clue objects, my view only shows one.

Thoughts? I have already, thanks to suggestions on stackoverflow, added to chapter.rb

attr_accessible :clues_attributes 

and had no luck, same behavior and errors with and without that.

Thanks in advance for your time

HappaGirl
  • 117
  • 1
  • 10

2 Answers2

1

I figured it out for myself. Not sure why exactly, I will speculate, someone is welcome to explain it better if I'm off.

The issue was here:

<%= form_for [@trail, @trail.chapters.build] do |f| %>

which I changed to:

<%= form_for @chapter do |f| %>

and then I had to change some things around in my chapters_controller to make the trails objects and capture the ids. But, after I made this change, my 3 clue fields started showing up in the view and my error about mass-assign went away.

I think the chapter I created before was empty and not really generated, only holding information, so trying to hold nested information with the clues form_for was another step of temporary data...Where as creating the object in my controller and then filling it with the form was more substantial....I know really technical...Like I said, I got it working, don't ask me how...but I'm beginning to understand how Rails thinks.

HappaGirl
  • 117
  • 1
  • 10
0

When I submit, I get the error: Can't mass-assign protected attributes: clue

This is telling you that the attribute is protected from mass assignment. Basically, the only way you would be able to set it is through a method in your code, not from user input. (Which get assigned, usually, through update_attributes on the model.)

What you need to do is add :clue to attr_accessible in models/chapter.rb.

You may want to add :clues as well - I think it should actually be giving you the error that :clues is protected. You may run into an issue with :clue_ids. Whatever it says is protected, just put in the attr_accessible method in that model and you should be able to update it from user input.

RFC1337
  • 167
  • 1
  • 7
  • 1
    Thanks for your response. I tried adding :clues and :clues_attributes to the chapter atrr_accessible and I got the same error: Can't mass-assign protected attributes: clue .... when I use :clue I get the error: unknown attribute: clue ...? – HappaGirl May 16 '12 at 15:11