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

Marie Veverka
Marie Veverka
12,117 Points

Creating relationships

I have been following along on the video, and when I put in this code: <strong><%= status.user.full_name %></strong> in the application.html.erb page I get an error.

It doesn't like it when I use first_name either. I am not sure where I am going wrong. I did add: def full_name first_name + " " + last_name to the user.rb page.

Also, the code: <strong><%= status.user.full_name %></strong> on the index.html.erb page and the code: <%= @status.user.full_name %> on the show.html.erb page is also receiving an error.

Also, none of the attr_accessible code will work for me. Any ideas on how I can fix these problems? Thanks!

9 Answers

Chris Dziewa
Chris Dziewa
17,781 Points

Which version of Rails are you using? If it's Rails 4 and above, attr_accessible has been replaced by something called strong parameters. Could you post your user.rb and status.rb files here in between 2 sets of three backticks (usually on same key as the tilda symbol). After the first set type rb and then put your code on the next line. and put the final backticks on their own line.

Marie Veverka
Marie Veverka
12,117 Points

Thanks Chris! I just installed the newest version but I bet that is the problem. Any advice on how I can adjust for this issue?

Chris Dziewa
Chris Dziewa
17,781 Points

When I first started with the Rails track on here I got really frustrated and then stopped and went to learn Ruby and Rails elsewhere. The thing that I realized is that Rails is just a gem and the version can be specified at any time. A good way to deal with this is to specify the version of Rails you want to use at the beginning when you start the app. This could be done like this: rails _3.2.14_ new treebook This tells Rails to generate a new app with the version between the underscores. To fix your existing app, all you have to do is go into your Gemfile and specify the rails gem with the version you want it to use. My gemfile uses this:

gem 'rails', '3.2.13'

Then you head to your terminal/console and type bundle This searches online to find and update your current "dependencies" or "gems". That is all it takes to update. Save yourself a lot of headache and use the rails version I listed and then head over after you have finished this tutorial to Michael Hartl's Rails Tutorial (hard word but worth it) where you will learn about strong parameters and building a similiar application but from scratch without using all of the rails generate commands. Note that you can learn about strong parameters and convert your app, but since you are just starting out it might be wise to learn the basics first. Plus in a job setting you will run into older versions, and knowing both attr_accessible and strong parameters would be worth a lot to you. I hope this helps you to understand better!

Marie Veverka
Marie Veverka
12,117 Points

I just checked - I'm using Rails 4.1.1

Chris Dziewa
Chris Dziewa
17,781 Points

I knew it haha! I did the same thing the first time!

Marie Veverka
Marie Veverka
12,117 Points

Okay here is my user.rb code:

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

  attr_accessible :email, :password, :password_confirmation, :remember_me,
                  :first_name, :last_name, :profile_name

  has_many :statuses

  def full_name
    first_name + " " + last_name
  end
end

and here is my status.rb code:

class Status < ActiveRecord::Base
    attr_accessible :content, :user_id
    belongs_to :user
end
Marie Veverka
Marie Veverka
12,117 Points

Oh man, I tried changing my gemfile and now I can't seem to get my app to reload. . .

Marie Veverka
Marie Veverka
12,117 Points

Is there a way I can get back to 4.1.1?

3.2.13 won't work because of some problems with railties and now I can't seem to get back to 4.1.1 I ran bundle install in terminal and everything seemed fine but when I run rails server it keeps taking me back to the prompt.

Chris Dziewa
Chris Dziewa
17,781 Points

What error message are you getting? To change it back you could change the version in gemfile back to 4.1.1 and run bundle to revert it back. Here is my gemfile which includes a few other additions for later videos you can try to copy and paste my code and run bundle again if you would like:

source 'https://rubygems.org'

gem 'rails', '3.2.13'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'


gem 'devise'
gem 'simple_form'
group :test do 
    gem 'shoulda'
    gem 'mocha'
end

group :development, :test do 
    gem 'sqlite3'
end

group :production do 
    gem 'pg'
end

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'
Marie Veverka
Marie Veverka
12,117 Points

hmm, not sure what is going on, but it is still not working. This time I just copied and pasted your code right into my gemfile. After that I ran bundle and then it instructed me to run bundle update. After that, I got this error. . .

An error occurred while installing pg (0.17.1), and Bundler cannot continue. Make sure that `gem install pg -v '0.17.1'` succeeds before bundling.

Thanks so much for trying to help Chris. I really appreciate it. I just have no idea what has gone wrong at this point.

Chris Dziewa
Chris Dziewa
17,781 Points

Try doing bundle install --without production

You'll only have to run the --without production flag once for your project. What this does is it ignores any gems in the group "production" in your gemfile. In this case it will ignore the 'pg' gem which is for Postgresql. That will only be important for the production app server such as the company Heroku.

Marie Veverka
Marie Veverka
12,117 Points

Hey Chris,

I just wanted to say thank for recommending the Rails Tutorial! It was super helpful!

Chris Dziewa
Chris Dziewa
17,781 Points

Good, I'm glad! I thought so as well when I was working on it. I still have to finish that one. I stopped at chapter 9 after I hit a wall with some of my tests failing. I'll probably get back to it once I have learned a bit more. Hope you don't run into any issues!

Marie Veverka
Marie Veverka
12,117 Points

Hi Chris,

I ran into that in chapter 8 and 9 as well. I recommend double checking your code against Michael's on github. Often times, my problem was actually just an extra end at the end of my tests. Thanks again!

Chris Dziewa
Chris Dziewa
17,781 Points

Yeah I figure it's a typo on my end! ha.

Chris Dziewa
Chris Dziewa
17,781 Points

It's funny how spending more and more time with a language or framework makes debugging that much easier. I went back to my Rails tutorial project and figured out in literally a couple minutes that I just had a capitalization error in one of my test cases. All my tests are green again ha. It's nice when you can just debug based on test errors. Thanks for recommending to go back to it!