Reputation: 21
I'm very new to rails. What I'm trying to do is display info i have in a seed file to my views/show page. This is some of my seed file, it's just made up organizations for a school project I'm doing.
`Organization.create(
name: "St. John's church",
location: "222 Bathurst st.",
description: "Church"
)
Organization.create(
name: "Women's Shelter Toronto",
location: "777 Yonge St.",
description: "Womens Shelter"
)
Organization.create(
name: "Toronto Homeless Shelter",
location: "111 King St.",
description: "Shelter"
)`
here is my show page
<h1>Organization#show</h1>
<div class="container">
<h1>Organization:</h1>
<%= @organization.name %>
</div><br>
<div class="container">
<h1>Description:</h1>
</div><br>
and organization controller
def show
@organization = Organization.all
end
def new
@organization = Organization.new
end
def create
@organization = Organization.new(organization_params)
if @organization.save
redirect_to root_url
else
render "new"
end
end
private
def organization_params
params.require(:organization).permit(:name, :description, :location)
end
end
This very basic i know i'm just learning.
Upvotes: 1
Views: 1215
Reputation: 33
In your OrganizationsController#show method you are setting @organization to a list of all organization entries in your database. @organization.name is therefore not a valid method. You can do one of two things. If you only want to show one organization on the show page, which would be typical, you would have to find that organization somehow. When a user requests a show page for a particular organization he typically does so in a RESTful manner, that is through /organizations/:id, where :id is the id of the particular organization he or she would like to show. In your controller try:
def show
@orginazation = Organization.find(params[:id])
end
params[] is how Rails passes information between views and controllers. In this case params[:id] would be 1 if the user requested /organizations/1. Your controller would then go to the database and find the organization whose id was 1.
Now if you want to show a list of all organizations you would do so typically in the #index method. Something like this.
def index
@organizations = Organization.all
end
Then you would have an index.html.erb
file in which you would iterate through the @organizations, like so.
<% @organizations.each do |organization| %>
<%= organization.name %>
<%= organization.location %>
<%= organization.description %>
<% end %>
This way when your user requests /organizations, he or she will see a listing of all the organizations in your database.
Edit: Your post also suggests that your controller is called OrganizationController
, it should be plural, OrganizationsController
and the associated filename should be organizations_controller.rb
Upvotes: 0
Reputation: 352
The purpose of the seeds.rb file is to populate the database after the first time it's created.
After you do that, you can display the newly created data in your database on the page using Active Record.
I see 2 possible problems here:
rake db:seed
- This command takes the seeds.rb file and actually creates the records in the database.Upvotes: 0
Reputation: 1642
First, you should probably use use create!
in your seed file so that if any of your creates fail you will see an exception flagged.
Second, what you are using as a show
action should more likely be the #index
action in your controller as that is the rails
convention. When you stray from rails
conventions your programming life gets a whole lot heavier and slower.
As an interim manual test you can run between first and second
is this: After your seed file has been run, you can do things like:
rails console
Organization.count
Organization.all
to verify that your data is seeded properly.
At that point, you may find implementing and debugging your show
, edit
and new
actions more straightforward.
Upvotes: 1