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 Creating an Authentication System Generating the Devise Views

Missing "attr_accessible" method from user.rb and status.rb before and after devise-gem install.

I'm having huge trouble in the "Build a simple Ruby on Rails Application" section between this stage (stage 3: 'Generating the Devise Views') and stage 4: 'creating relationships'.

It relates to the "attr_accessible" method.

I've followed along with every step, and this is my second attempt starting from scratch from the beginning of 'Build a Simple Ruby on Rails Application'.

Before and after install of devise, the attr_accessible method and associated lines of code are nowhere to be found in the app->model->user.rb or app->model->status.rb.

Here is my code from app->model->user.rb :

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

Here is my code from app->model->user.rb :

class Status < ActiveRecord::Base
  belongs_to :user
end

When I try and add the code from the project files/video and make both status.rb and user.rb look identical to the video, I get the following error:

undefined method `attr_accessible'.

The att_accessible portion i've been adding to these files from the project files download from stage 4: 'creating relationships'.

user.rb

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me,
                  :first_name, :last_name, :profile_name

and status.rb

class Status < ActiveRecord::Base
  attr_accessible :content, :user_id
  belongs_to :user
end

In terms of functionality before up until i needed to associate the user_id with first_name, I had no errors. Especially regarding mass user security issues both instructors seem to hit. The application worked fine until associating the user_id with the first_name in app->views->statuses->show.html.erb

When I added the association I got another error saying first_name was nil. I looked up the user_id I was logged in under (number 3) and made sure to input that into the user_id form field. I tried all numbers from 0 though 20 still with no luck.

I am stuck! Can anyone offer some answers as to why attr_accessible isn't showing up at all / anything!!

I think the steps in this video are my root problem. As I said, i've been through it twice from scratch, using different names for each application, same results both times.

Operating system OS X version 10.9.2 Ruby version: 2.1.1

8 Answers

Roberto Alicata
PLUS
Roberto Alicata
Courses Plus Student 39,959 Points

Yes it's correct

gem install rails --version=3.2
rbenv rehash

then try

rails -v

Everything was going great until :

gem install rails --version=3.2

It starts to install until:

ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /usr/bin directory.

Also ruby -v returns ruby 2.0.0p451 as opposed to 1.9.3-p194 even though rbenv version output is:

* 1.9.3-p194 (set by /Users/Tom/.rbenv/version)
  2.0.0-p353
  2.1.1

Will any new application still use 1.9.3?

**ADDED

I'v even gone ahead and uninstalled any other versions of Ruby, leaving only 1.9.3 and "ruby -v" still returns ruby 2.0.0p451

**ADDED.. again

used sudo class and that fixed the permissions. But ruby-v still outputs a version of ruby which is no longer installed!

**ADDED.. again.. again

Restarted the shell/cmd window and everything is outputting great.

Roberto Alicata
PLUS
Roberto Alicata
Courses Plus Student 39,959 Points

Are you using Rails 4? If you had this error

 "`attr_accessible` is extracted out of Rails into a gem. Please use new recommended protection model for params(strong_parameters) or add `protected_attributes` to your Gemfile to use old one."

you need to install protected_attributes gem

Add this line to your application's Gemfile:

gem 'protected_attributes'

And then execute:

$ bundle

then you can use:

attr_accessible  :content, :user_id

Thanks for your response, let me give this a try now!

I've run the instructions you gave, and I got a little further - it didn't error straight away after I added attr_accessible to status.rb and user.rb.

When I create the association in show.html.erb (reference stage 4 - creating relationships)

<p>
  <b>Name:
    <%= @status.user.first_name %>
    </b>
</p>

and then write and submit a status using the id 3 (the user_id i am logged in as - checked this in db by running ruby console and

user = User.first

).

When I get to the show page after pressing submit, I get this error: undefined method `first_name' for nil:NilClass

and it's highlighting <%= @status.user.first_name %> in the show.html.erb file.

I'm using Rails 4.1.0 and Ruby 2.0.0-p451.

I realise that these versions are different from the video hence it's not working as described. What I've been trying to do is find, download and finally run the appropriate versions on my machine.

My mbp is running mavericks so rails-installer dies when i try and install from there.

Can you point me to how I might create a version-setup to match the video? And then how would I select that specific version to run?

I've had a look at the "building a todo list with rails 4" project.. I was like YES! Come on, it must be the right version.. and then at the beginning of the video "we're using rails 4.0.1... This tutorial may not work with rails 4.1." GAHHHH!!

Again, I need to download, and run a specific version of rails to match the video. Can I still use ruby 2.0.0-p451?

I'm getting so bogged down with all this.. any clarification would be hugely, hugely appreciated!!

Roberto Alicata
PLUS
Roberto Alicata
Courses Plus Student 39,959 Points

If you want to use Rails4 try to remove the gem 'protected_attributes' from the Gemfile and rerun bundle and remove all the attr_accessible on your user model.

Then add this code to app / controllers / application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :profile_name, :email, :password, :password_confirmation) }
  end  
end

If you want to use the same version of ruby and rails as the video, use RVM Ruby Version Manager. RVM is a command-line tool which allows you to easily install, manage, and work with multiple ruby environments from interpreters to sets of gems.

Do I keep

attr_accessible :content, :user_id  

in status.rb?

Thanks for the tip with RVM - I'll investigate!

So RVM and rbenv are both used for ruby management?

Thanks for all your help so far, it's great!!

I'm going for another clean start to match the same versions as both project videos.

On my computer now I have:

  • rbenv
  • rails 4.1.0
  • ruby 2.0.1-p451

For building a simple Ruby on Rails Application I need:

  • ruby 1.9.3-p194
  • rails 3.2 ( think?)

I would install rails v 1.9.3-p194 by:

rbenv install 1.9.3-p194

I could then say something like

rbenv global 1.9.3-p194

And double check with

rbenv versions

and that particular version of ruby would start running throughout my computer (let's avoid local to keep it simple for now?)

So I think i've got the ruby install together with rbenv.

But how would is start with installing the correct version of rails?

Ah, I would say

       gem install rails --version=3.2

?

Hey Thomas, I had pretty much all the issues you did. In the end I went and read through the documentation at api.rubyonrails.org to gain a better understanding of the fundamentals of how RoR works, rather than following along just trying to make code tests pass. With a better understanding I was able to better understand the advice given in this forum and figure out solutions for myself.

You should be able to do the treebook app with the latest versions of everything now that simple_form has been updated to work with bootstrap3, you will need to use the strong params in the rails 4 docs above and check the info for devise as i am unsure whether it is updated for rails 4.

regardless of versions you will gain a much more widely applicable knowledge base by learning to read and understand the documentation associated with the various components you are using. Treehouse is awesome to enhance your knowledge!

good luck

Roberto Alicata
Roberto Alicata
Courses Plus Student 39,959 Points

I agree with you, in fact I'm doing your same path, but I think that is difficult to deal with all these "errors" for someone who approaches for the first time in Rails

Great advice, thanks for your response.

It's a really strange track with regards to the order they go through the material. For instance, Ruby foundations, console foundations and git comes after you use them in a practical situation. To me that seems a little bit top-down!

I went back and installed a previous version of Ruby and Rails to run through the course - at this point, mostly so I could feel like i've accomplished something lol. I have the full intention to come back and revisit this particular badge when I've got a better understanding of the latest changes/RoR to see what else I can do!

Thanks again :)

Yes please!

I'm now at a great starting point to follow the project thanks to your great advice!!

Thanks you so much