Question

I came back to the relatively "old book" Head First rails, which was published for Rails 2.3. Now, going back again through those samples and using Rails 3 I came up with some questions.

Let's say that I'm adapting the sample for coconut airways and instead of flights and seats, I have a project and tasks.

The page shows a project description and below a list of tasks associated to that project. so far so good. now below that there is a form to create new task. This task needs a Task object and the project_id. here is when things do not work as before.

if you want to do it like the old style you will type:

<%= render :partial => "new_task", 
    :locals =>  {:task => Task.new(@project.id)} %>

well, this is showing the mass-assign error.

Then I tried to pass both as parameter:

<%= render :partial => "new_task", 
    :locals =>  {:task => Task.new, :project_id  => @project.id} %>

and assign it in the partial

<%= f.hidden_field :project_id, :value => project_id %>

any hint?

EDITED:

class Task < ActiveRecord::Base belongs_to :project attr_accessible :title end

class Project < ActiveRecord::Base has_many :tasks attr_accessible :description, :title end

Was it helpful?

Solution

If you change your model's attr_accessible you can include these assignments to be made. For more information about attr_accessible and mass assignment see: Ruby on Rails API

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top