How do I get an id from a student in a list and display it the student's information on another page? Ruby on Rails

user6232145

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

Table's Page

<% @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
Zack Strickland

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get array of student's id

From Dev

How do I get the ID of a post from another controller in Rails?

From Dev

How to get the student id where student got grade A but not C

From Dev

How to display multiple student name in zii.widgets.grid.CGridView from database in admin page?

From Dev

How to display multiple student name in zii.widgets.grid.CGridView from database in admin page?

From Dev

Making a list from the position number of another list in Racket Advanced Student

From Dev

How to dispaly a login student written message in ruby on rails

From Dev

How to add item to chosen student from list and get an empty list in class?

From Dev

How to add item to chosen student from list and get an empty list in class?

From Dev

Rails 4: how do I get ID on the index page?

From Dev

Removing student from linked list

From Dev

How do I pass the post id from the url to another page?

From Dev

How can I select classmates of a given student from a student-class association table?

From Dev

Link list program to Display Student marks

From Dev

How do I give student score that are equal same rank

From Dev

Rails - Couldn't find Student with 'id'=

From Dev

How do I get Rails to display standard useful information in log statements?

From Dev

How do I get id number from another table?

From Dev

Column 'student_id' in field list is ambiguous

From Dev

How do I get the posts from single page custom post type from a sidebar to display in a page?

From Dev

How to display a string with quotes in racket Beginning Student

From Dev

How do I get information from a UITableViewCell?

From Dev

How to get the information from table to another page using PHP and MYSQL?

From Dev

Remove null objects from a List of Student objects

From Dev

Delete Single Student from Linked List

From Dev

How do i get a member's id from their tag discord?

From Dev

How do I get information from the view to the controller and back to another view?

From Dev

How do I display a summary of items in a cart from a $_SESSION onto another page

From Dev

How do I save the information I filled out on a form from one activity to another and display multiple attempts in my Android Program?

Related Related

  1. 1

    Get array of student's id

  2. 2

    How do I get the ID of a post from another controller in Rails?

  3. 3

    How to get the student id where student got grade A but not C

  4. 4

    How to display multiple student name in zii.widgets.grid.CGridView from database in admin page?

  5. 5

    How to display multiple student name in zii.widgets.grid.CGridView from database in admin page?

  6. 6

    Making a list from the position number of another list in Racket Advanced Student

  7. 7

    How to dispaly a login student written message in ruby on rails

  8. 8

    How to add item to chosen student from list and get an empty list in class?

  9. 9

    How to add item to chosen student from list and get an empty list in class?

  10. 10

    Rails 4: how do I get ID on the index page?

  11. 11

    Removing student from linked list

  12. 12

    How do I pass the post id from the url to another page?

  13. 13

    How can I select classmates of a given student from a student-class association table?

  14. 14

    Link list program to Display Student marks

  15. 15

    How do I give student score that are equal same rank

  16. 16

    Rails - Couldn't find Student with 'id'=

  17. 17

    How do I get Rails to display standard useful information in log statements?

  18. 18

    How do I get id number from another table?

  19. 19

    Column 'student_id' in field list is ambiguous

  20. 20

    How do I get the posts from single page custom post type from a sidebar to display in a page?

  21. 21

    How to display a string with quotes in racket Beginning Student

  22. 22

    How do I get information from a UITableViewCell?

  23. 23

    How to get the information from table to another page using PHP and MYSQL?

  24. 24

    Remove null objects from a List of Student objects

  25. 25

    Delete Single Student from Linked List

  26. 26

    How do i get a member's id from their tag discord?

  27. 27

    How do I get information from the view to the controller and back to another view?

  28. 28

    How do I display a summary of items in a cart from a $_SESSION onto another page

  29. 29

    How do I save the information I filled out on a form from one activity to another and display multiple attempts in my Android Program?

HotTag

Archive