Reputation: 1627
I am having a little problemo here. I have a model, view and controller. I go from the index.html.erb to the animals.html.erb. (Assume from my posted code this has already happened) On the animals view I would like to display some data represented by my Profile model.
Here is my code.
class Profile < ActiveRecord::Base
has_many :animals
validates :firstname, presence: true
validates :lastname, presence: true
validates :address, presence: true
validates :city, presence: true
validates :state, presence: true
validates :zip, presence: true
validates :gender,
presence: true,
length: {minimum: 1, maximum: 2 }
validates :breeder, presence: true
end
class Animal < ActiveRecord::Base
belongs_to :profile
validates :fullname, presence: true
validates :profileID, presence: true
validates :age,
presence: true,
length: {minimum: 1, maximum: 3 }
validates :gender,
presence: true,
length: {minimum: 1, maximum: 2 }
validates :purrfactor, presence: true
:weight
:height
:length
validates :fixed, presence: true
validates :shots, presence: true
validates :papers, presence: true
validates :created_at, presence: true
validates :updated_at, presence: true
end
class HomeController < ApplicationController
def index
end
def show
@profile = Profile.find(1)
end
end
<div class="container-fluid">
<div class="row">
<p><%= @profile.firstname %></p>
</div>
</div>
Profile Load (0.2ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."id" = ? LIMIT 1 [["id", 1]]
I don't want anyone to solve my problem per say. What I have learned thus far about RoR, this should work. However, it does not bring back the firstname model value from my Sqlite3 db. Is the SQL generated by my find statement correct? Are my models messed up? Just looking for some advice to steer me in the right direction to fix this issue. Also, should I have the ID referenced in my models (Unique ID in my db table) Big thanks in advance!
I would think that the home/show route would work. The link_to tag goes to the correct action and renders the show page without error.
<%= link_to "Get Started!", {:controller => 'home', :action => 'show'}, { class: "btn btn-primary" } %>
get "home/index"
get "home/show"
home_index GET /home/index(.:format) home#index
home_show GET /home/show(.:format) home#show
root GET / home#index
I cleaned up my code a bit. Got rid of the unneeded Animals Controller, as well as, the routes. I updated my post, to show these revisions.
I found that the generated SQL was valid and returned an Active Record, however it was not being displayed on the View. After removing the attr_accessible attribute from the Profiles.rb model class the value displayed.
Why did the attr_accessible attribute block the data from displaying on the view? I thought this attribute was only for mass assignment security. I.E. updates, saves, etc...
Upvotes: 0
Views: 84
Reputation: 184
You do not need :id
in your models because they will be generated automatically. In my opinion, if the routes are correct and the Animals View rendered on home#show
, then everything is probably fine in RoR and just Profile with :id 1
does not exist in your DB.
Upvotes: 1