No route matches [POST] "/home/new"

196 views
Skip to first unread message

Bob Tian

unread,
Jan 26, 2016, 9:56:10 PM1/26/16
to rubyonra...@googlegroups.com
Hello all, I'm new to ruby and I am having trouble with adding new data
(name, height, weight, etc). Everytime I try to save my data, I get
this error:

No route matches [POST] "/home/new"
Rails.root: C:/Users/Jeffrey/blog

Request

Parameters:

{"utf8"=>"✓",
"authenticity_token"=>"Vgkmt3PPEobQFpJyWNIoHNnXtkmq2Uyk4vjMvgl8ZdhwIvc516BzXUfDs05OZ/8LLfkyKTxca90qiA2LBYBnwQ==",
"@input"=>{"name"=>"asd",
"weight"=>"233",
"height"=>"23",
"color"=>"red",
"age"=>"23"},
"commit"=>"Save @input"}


I am able to view index and enter the information, but when I submit, I
get this problem. Any help would be appreciated! Thank you
=================================================================
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)
if @article.save
redirect_to @input
else
render 'new'
end
end

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

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

def update
@input = Person.find(params[:id])
respond_to do |x|
if @input.update(input_params)
x.html {redirect_to :action => 'index'}
else
x.html {render :edit}
end
end
end

private

def input_params
params.require(:inputs).permit(:name, :weight, :height, :color,
:age)
end
end

=================================================================
new.html.erb

<h1>New People</h1>

<%= render 'form' %>

<%= link_to 'Back', home_index_path %>

=================================================================
_form.html.erb

<%= form_for :@input do |person| %>
<div class="field">
<%= person.label :name %><br>
<%= person.text_field :name %>
</div>
<div class="field">
<%= person.label :weight %><br>
<%= person.number_field :weight %>
</div>
<div class="field">
<%= person.label :height %><br>
<%= person.number_field :height %>
</div>
<div class="field">
<%= person.label :color %><br>
<%= person.text_field :color %>
</div>
<div class="field">
<%= person.label :age %><br>
<%= person.number_field :age %>
</div>
<div class="actions">
<%= person.submit %>
</div>
<% end %>

=================================================================
index.html.erb

<p id="notice"><%= notice %></p>

<h1>Listing People</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',home_path(person.id) %></td>
<td><%= link_to 'Edit', edit_home_path(person.id) %></td>
</tr>
<% end %>
</tbody>
</table>

<br>
<%= link_to 'New Person', new_home_path %>

=================================================================

my routes.db has

resources :home
root 'home#index'

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

Justin Yoon

unread,
Jan 27, 2016, 1:43:23 AM1/27/16
to rubyonra...@googlegroups.com
I'm pretty new to ruby as well but I think the problem is that the Rails router doesn't know what to do with a POST request to "/home/new". I think you just have to add this line to routes.rb:

post 'home/new', to: 'home#new'

Like I said I'm a newbie myself so I'd love it if someone could correct me if I'm wrong or let me know if I got it all.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/81a90dccf0c3e3eccd221174d5232dcc%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Bob Tian

unread,
Jan 27, 2016, 2:59:30 AM1/27/16
to rubyonra...@googlegroups.com
I dont get the error, but when I try to save the data, it seems to
reload the page again, and the data doesnt get saved. It doesn't appear
at all when i go back to /home

Mike

unread,
Jan 27, 2016, 3:11:49 AM1/27/16
to Ruby on Rails: Talk
You have an error in your controller

You have @article.save in the create action, rather than @input.save

Additionally it is convention to use the same name of the variable as the model, so you should really use @person = Person.new, rather than @input = Person.new

You can always check what routes are recognised by use of the 'rake routes' command

Bob Tian

unread,
Jan 27, 2016, 1:55:20 PM1/27/16
to rubyonra...@googlegroups.com
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>

Colin Law

unread,
Jan 27, 2016, 5:11:50 PM1/27/16
to Ruby on Rails: Talk
On 27 January 2016 at 18:54, Bob Tian <li...@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

Look in log/development.log and when you click delete you should see
the request and whether it is calling the destroy method. You can
insert diagnostic code in your methods using stuff like
def destroy
logger.info "In destroy"
@person = Person.find(params[:id])
logger.info @person.inspect
@person.destroy
redirect_to :action => :index
end

and your logger output will appear in the log file.

Colin

Mike Simkins

unread,
Jan 27, 2016, 5:58:34 PM1/27/16
to rubyonra...@googlegroups.com
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
--
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-ta...@googlegroups.com.

To post to this group, send email to rubyonra...@googlegroups.com.

Bob Tian

unread,
Jan 27, 2016, 8:49:11 PM1/27/16
to rubyonra...@googlegroups.com
Mike S. wrote in post #1180984:
> 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

I have the changes and it just directs me to the show page displaying
the info like before. When I go back to /people the data is still there.
In the logs it says:

Started GET "/people/1?method=delete" for ::1 at 2016-01-27 17:38:42
-800
Processing by PeopleController#show as HTML
Parameters: {"method" => "delete", "id" => "2"}
Person Load (0.5ms) SELECT "people".* FROM "people" WHERE "people"."id"
= $1 LIMIT 1 [["id", 2]]
Rendered people/show.html.erb within layouts/application (0.0ms)
Completed 200 OK in 26ms (views: 25.0 ms | ActiveRecord :0.5 ms)

...

Started GET "/stylesheets/default.css" for ::1 at ..

...

Started GET "/javascripts/default.js" for ::1 at ..


I did some searching and I was wondering if it is the javascript because
I changed the application.html.erb from

<%= stylesheet_link_tag 'application'
<%= javascript_include_tag 'application'

and replaced 'application' to 'default' since I am on windows and the
'application' was causing problems

Mike Simkins

unread,
Jan 28, 2016, 4:48:36 AM1/28/16
to rubyonra...@googlegroups.com
I assume therefore you also renamed the /app/assets/javascripts/application.js to default.js

sorry I do not develop under Windows, so cannot be sure if there is not some other issue related to that, there are a number of shortcomings when you use rails on Windows, but that is mainly gem support, not JavaScript

Just as an interest, can you create a blank application, and do something like

rails g scaffold something field1 field2

And then check the code it generated in the views - I doubt it is different on Windows, but I cannot be sure

Also run the app and see if you can add/delete ok


Sent from my iPad
> --
> 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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/333623f4c1f2f74c51c21683ef97854d%40ruby-forum.com.

Bob Tian

unread,
Jan 28, 2016, 12:25:42 PM1/28/16
to rubyonra...@googlegroups.com
I actually did not rename it to default.js, but when I try to do that
and access localhost I get an error:

ExecJS::ProgramError in People#index
Showing: /app/views/layouts/application.html.erb where line #6 raised:
TypeError: Object doesn't support this property or method

and line 6 which is:

<%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>


I tried using scaffold and it does the same thing, it can add, but it
does not delete, it directs me to /posts/1 where it just displays the
data for id:1

The code in views is different from mine:

index:

<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are
you sure?' } %></td>

controller:

def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was
successfully destroyed.' }
format.json { head :no_content }
end
end

Bob Tian

unread,
Jan 28, 2016, 12:49:41 PM1/28/16
to rubyonra...@googlegroups.com
Hm, I found this from stackoverflow
http://stackoverflow.com/questions/7281907/rails-3-1-issue-with-javascript-include-tag-in-application-html-erb

and if I remove //= require_tree and //= turbolinks

then I can include application.js and the add/delete does work for
scaffold, but I still cannot remove people

Mike Simkins

unread,
Jan 28, 2016, 1:02:56 PM1/28/16
to rubyonra...@googlegroups.com
So something is definitely broken in your Javascripts, or the views - the scaffold code there is what I am used to seeing, just referencing the record rather than the explicit ID you pass in.

I am at a loss as to why Windows is complaining when you change the name, or indeed why it would not respond to the standard name of application.js - which has no meaning to Windows that I am aware of
> --
> 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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/460fe4de0315f223782195ada5312029%40ruby-forum.com.
Reply all
Reply to author
Forward
0 new messages