Ruby on Rails Tuesday, January 26, 2016

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.

On Tue, Jan 26, 2016 at 6:55 PM, Bob Tian <lists@ruby-forum.com> wrote:
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/.

--
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-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/81a90dccf0c3e3eccd221174d5232dcc%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

--
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-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/CAEe%2BqudwzpQMm7P1YOUjQbgYXxD-Bcys6VptxXbGoK9P4xDSZw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment