My home_controller.rb
class HomeController < ApplicationController
def index
@inputs = Person.all
end
def new
@input = Person.new
end
def create
@input = Person.new(input_params)
respond_to do |x|
if @input.save
x.html {redirect_to :action => 'index'}
else
x.html {render :action => 'new'}
end
end
end
def show
@input = Person.find(params[:id])
end
private
def input_params
params.require(:inputs).permit(:name, :weight, :height, :color, :age)
end
end
My routes file only have two lines:
resources: home
root 'home#index'
My index.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th> Weight</th>
<th> Height</th>
<th> Color</th>
<th> Age</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @inputs.each do |person| %>
<tr>
<td><%= person.name %></td>
<td><%= person.weight %></td>
<td><%= person.height %></td>
<td><%= person.color %></td>
<td><%= person.age %></td>
<td><%= link_to 'Show',person %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Test', new_home_path %>
my show.html.erb:
<p>
<strong>Name:</strong>
<%= @person.name %>
</p>
<p>
<strong>Weight:</strong>
<%= @person.weight %>
</p>
<p>
<strong>Height:</strong>
<%= @person.height %>
</p>
<p>
<strong>Color:</strong>
<%= @person.color %>
</p>
<p>
<strong>Age:</strong>
<%= @person.age %>
</p>
<%= link_to 'Back', index_home_path %>
Also, here is a rake routes result from my case: enter image description here
Aucun commentaire:
Enregistrer un commentaire