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