I am creating a list of students and when I click on one of the hyperlinks, I want to display a table with the student's information I clicked on.
I am working with two pages, index.html.erb and show.html.erb. In the index page, I want to display the list and the show page, I want to display the information for only one student. I am very new to Ruby on Rails, but I want to get better at it. Thank for all your responses.
index.html.erb
<h1> Welcome </h1>
<%= link_to "Display Table", students_show_path %>
<%= link_to "Form", students_new_path %>
<ol> Students
<% @student.each do |s| %>
<!--<li><%= link_to s.FirstName, students_show_path(@student.students_id), method: :post %> </li> -->
<li><%= link_to s.FirstName, :action => "show", :id => Student.id %> </li>
<% end %>
</ol>
This is my show.html.erb
<% @student.each do |s| %>
<tr>
<td> <%= s.FirstName %> </td>
<td> <%= s.LastName %> </td>
<td> <%= s.NickName %> </td>
<td> <%= s.EmailAddress %> </td>
<td> <%= s.Birthday %> </td>
<td> <%= s.MedicalNotes %> </td>
<td> <%= s.Grade %> </td>
<td> <%= s.School %> </td>
<td> <%= s.Gender %> </td>
</tr>
This is my routes.rb
Rails.application.routes.draw do
root 'students#index'
get 'students/index'
get 'students/show'
get 'students/new'
get 'students/update'
get 'students/create'
get 'students/edit'
get 'students/destroy'
get '/signup', to: 'students#new'
post '/signup', to: 'students#create'
post '/index', to: 'students#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :students
# generates:
# get "/students" -- index on your controller
# get "/students/:id" -- show on your controller
# get "/students/new" -- new method on your controller
# post "/students" -- create on your controller
# get "/students/:id/edit" -- edit method on your controller
# put "/students/:id" -- update on your controller
# patch "/students/:id" -- update on your controller
# delete "/students/:id" -- destroy on your controller
end
This is my students_controller
class StudentsController < ApplicationController
def index
@student = Student.all
end
def show
id = params[:id]
@student = Student.where("Student id = ?" , "#{id}")
end
def new
@student = Student.new
end
def update
end
def create
@student = Student.new(student_params)
if @student.save
redirect_to students_show_path
else
render 'new'
end
end
def edit
end
def destroy
end
private
def student_params
params.permit(:FirstName, :LastName, :NickName, :EmailAddress, :Birthday, :MedicalNotes, :Grade, :School, :Gender)
end
end
First thing:
The Ruby way to name attributes, methods, and variables is to use snake_case. For example, s.FirstName
should be s.first_name
. In Ruby, if the name is capitalized like FirstName
it is actually a constant. This style is called CamelCase and it is the conventional way to name classes in Ruby e.g. StudentsController
.
For the routes.rb
:
Rails will generate all of the RESTful routes and helper methods for your resource if you use
resources :students
you can then view all of these routes by running rake routes
on the command line or visiting http://localhost:3000/rails/info/routes
in the browser. You can learn more info about routes in the documentation: http://guides.rubyonrails.org/routing.html#inspecting-and-testing-routes
You can then use the Rails link_to
helper method like this:
<%= link_to s.first_name, s %>
and Rails will figure out the id
of the object for you. Or if you want to be more explicit:
<%= link_to s.first_name, student_path(s.id) %>
And in the show
action of your StudentsController
you want:
def show
@student = Student.find(params[:id])
end
this will look up the student record in the database based on the :id
parameter in the URL for student show page
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments