mardi 26 janvier 2016

Ruby On Rails Basic Show method failed

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 %>

The error i get is: It seems like i am messing with the routes, because my route did not direct me to the correct path, i am newbie to ror, please help me with this. i have try the get'home/:id', to: 'home#index' it does not work.

Also, here is a rake routes result from my case: enter image description here

Aucun commentaire:

Enregistrer un commentaire