Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Build a Simple Ruby on Rails Application Customizing Forms Creating Relationships

Rodrigo Soares
Rodrigo Soares
2,460 Points

attr_protected atribute is still allowing mass assignment...

Hello guys, I'm following the tutorial development of treebook and I'm now facing a problem.

We are adding the user_id field into the New Status form and we don't want to allow users to assign the user_id to the post. The problem is even if I declared attr_protected :user_id in my model the view still let's me assign that attribute...

Can I get any adivce on what can be possibly be wrong?

Thanks a lot.

PS:

My model for users.rb

```class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable

attr_accessible :first_name, :last_name, :profile_name attr_protected :user_id end

My view for New status:

```<%= simple_form_for(@status, html: {class: "form-horizontal"}) do |f| %>
  <% if @status.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@status.errors.count, "error") %> prohibited this status from being saved:</h2>

      <ul>
      <% @status.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.input :user_id %>
  <%= f.input :content %>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

1 Answer

The problem may have to do with your code: <%= f.input :user_id %> You are not passing "collection" option of Simple_form.

Try replacing with this code and see if it works: <%= f.input :user_id, collection: User.all, label_method: :full_name %>

you can read about the "collection" option here: https://github.com/plataformatec/simple_form