Jake Corn
Jake Corn

Reputation: 3

Rails - Object Available in View, but Not Object's properties?

I've got an unusual problem where I can access an Object, but not its properties in a view. Any Assistance, guidance, is greatly appreciated.

This is my model (app/models/team_member.rb):

class TeamMember < ApplicationRecord
  attr_accessor :name
end

This is my controller (app/controllers/static_controller.rb):

class StaticController < ApplicationController
  def index
    @team_members    = TeamMember.all
  end
end

This is my seeds file (db/seeds.rb):

puts 'POPULATING TEAM MEMBERS'
team_member = TeamMember.create! :name => 'Jones Namerson'
team_member = TeamMember.create! :name => 'John Johnson'
team_member = TeamMember.create! :name => 'Billy Bob'
team_member = TeamMember.create! :name => 'Tom Thompson'

This is my view (app/views/static/index.html.erb):

<div class="team-member">
          <a href="#bioModal1" class="portfolio-link" data-toggle="modal">
            <img src="http://www.canyon-news.com/wp-content/uploads/2015/09/Pope-Francis-1.jpg" class="img-responsive img-circle">
            <h4><%= @team_members[0] %></h4>
            <p class="text-muted">Co-Owner</p>
          </a>
        </div>

This produces output like:

<image>THE IMAGE</image>

#<TeamMember:0x007fdd5e08c288>
Co-Owner

So, I'm able to access the Object in the view but My Goal is to access the objects properties. I'll make a change to my view by adding what I actually want to see:

<div class="team-member">
          <a href="#bioModal1" class="portfolio-link" data-toggle="modal">
            <img src="http://www.canyon-news.com/wp-content/uploads/2015/09/Pope-Francis-1.jpg" class="img-responsive img-circle">
            <h4><%= @team_members[0].name %></h4>
            <p class="text-muted">Co-Owner</p>
          </a>
        </div>

but this produces:

<image>THE IMAGE</image>

Co-Owner

So, basically my object is available in the View, but not its properties? Any Help is greatly appreciated.

Upvotes: 0

Views: 272

Answers (2)

mu is too short
mu is too short

Reputation: 434805

You have this in your model:

attr_accessor :name

You also have a name column in your database. The attr_accessor :name is essentially shorthand for this:

def name
  @name
end
def name=(s)
  @name = s
end

but ActiveRecord usually supplies accessor and mutator methods for database-backed attributes and you've supplied your own name and name= methods (via attr_accessor) that don't know anything about the name column in your database.

Remove the attr_accessor :name call from your TeamMember class and your problem should go away.

Upvotes: 2

Carlos C
Carlos C

Reputation: 677

Line in your TeamMember class "attr_accessor :name" is not necessary. Unlike regular Ruby classes, you are creating a subclass of ApplicationRecord, so as long as you have your TeamMember migration in place Active Record takes care of it.

For example, if you run something like "rails g model MyModel my_property:string" followed by "rake db:migrate", Rails creates a MyModel class that extends application record and a my_models table with a name field. MyModel class gets accessors for the name property for free without adding any code. Thus adding again like you are doing might be creating an expected behavior

Finally, if you just want to access the first object, you can replace @team_members = TeamMember.all by @team_member = TeamMember.first. This is just an enhancement.

I'd suggest testing it using "rails c"

TeamMember.all
TeamMember.first
TeamMember.first.name

Upvotes: 0

Related Questions