Ruby on Rails Wednesday, January 27, 2016

In the delete link, you need to use the delete method-Rails now expects a delete verb, where as your code is generating a get I believe

So

<%= link_to 'Delete', :controller => "people", :action
=> "destroy", :id => e, :method => :delete %>


Sent from my iPhone

On 27 Jan 2016, at 18:54, Bob Tian <lists@ruby-forum.com> wrote:

Hello, thanks to you I have been able to add in the data. I also changed
and cleaned up the code and used the convention with regards to the
variables. I have another question regarding the destroy/deleting data,
when I click on delete, it directs me to show where i can view the data,
but does not delete anything. Here is my updated code


peoplecontroller:
class PeopleController < ApplicationController

   def index
       @people = Person.all
   end


   def show
       @person = Person.find(params[:id])
   end


   def new
       @person = Person.new
   end


   def create
       @person = Person.new(person_params)
       @person.save
       redirect_to :action => :index
   end


   def edit
       @person = Person.find(params[:id])
   end


   def update
       @person = Person.find(params[:id])
       @person.update(person_params)
       redirect_to :action => :show, :id => @person
   end


   def destroy
       @person = Person.find(params[:id])
       @person.destroy
       redirect_to :action => :index
   end

   private
   def person_params
       params.require(:person).permit(:name, :weight, :height, :color,
:age)
   end
end

======================================================================
index:

<h1> People list</h1>
<table>
   <thead>
       <tr>
           <th>Name</th>
           <th> Weight</th>
           <th> Height</th>
           <th> Color</th>
           <th> Age</th>
           <th colspan="4"></th>
       </tr>
   </thead>
   <tbody>
       <% @people.each do |e| %>
       <tr>
           <td><%= e.name %></td>
           <td><%= e.weight %></td>
           <td><%= e.height %></td>
           <td><%= e.color %></td>
           <td><%= e.age %></td>
           <td><%= link_to 'Show', :controller => "people", :action =>
"show", :id => e %></td>
           <td><%= link_to 'Edit', :controller => "people", :action =>
"edit", :id => e %></td>
           <td><%= link_to 'Delete', :controller => "people", :action
=> "destroy", :id => e %></td>
       </tr>
       <% end %>
    </tbody>
</table>
<br>
<%= link_to 'New Input', :controller => 'people', :action => 'new' %>

=====================================================================
show.html.erb


<p id="notice"><%= notice %></p>
<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>

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-talk/Kf-1pDBEmK0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5bf60a333e1bef4196df3b3077774be4%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment