Getting Started with Rails guide - getting errors in step 5.12 Using partials

72 views
Skip to first unread message

Brian Burns

unread,
Feb 27, 2016, 1:20:28 PM2/27/16
to Ruby on Rails: Documentation
After changing the edit and new forms to use the _form partial, I get this error on trying to edit a record (I changed article to artwork for the examples):
No route matches [POST] "/artworks/2/edit"
Rails.root: c:/Users/bburns/Desktop/arttour
Application Trace | Framework Trace | Full Trace 
Routes
Routes match in priority from top to bottom
Helper     HTTP Verb     Path     Controller#Action
Path / Url            
welcome_index_path     GET     /welcome/index(.:format)     welcome#index
artworks_path     GET     /artworks(.:format)     artworks#index
    POST     /artworks(.:format)     artworks#create
new_artwork_path     GET     /artworks/new(.:format)     artworks#new
edit_artwork_path     GET     /artworks/:id/edit(.:format)     artworks#edit
artwork_path     GET     /artworks/:id(.:format)     artworks#show
    PATCH     /artworks/:id(.:format)     artworks#update
    PUT     /artworks/:id(.:format)     artworks#update
    DELETE     /artworks/:id(.:format)     artworks#destroy
root_path     GET     /     welcome#index
Request
Parameters:
{"utf8"=>"?",
 "authenticity_token"=>"0+8G5XLT4b6/3U1v6K4p0GnpcSxjsti8wid/+akAlDrFEQEfeVvnwp9Jte+ohyJGFUIw0vqYGnPcQxAP4rEijA==",
 "artwork"=>{"artist"=>"inness",
 "year"=>"1889",
 "title"=>"moonrise",
 "about"=>""},
 "commit"=>"Save Artwork"}
And adding a new record gets a similar error -

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

I'm new to Rails so not sure what might be going wrong - thanks in advance for any pointers 

routes.rb:


Rails.application.routes.draw do
  get 'welcome/index'
  resources :artworks
  root 'welcome#index'
end

artworks_controller.rb:

class ArtworksController < ApplicationController
 
  def index
    @artworks = Artwork.all
  end
 
  def show
    @artwork = Artwork.find(params[:id])
  end
 
  def new
    @artwork = Artwork.new
  end

  def edit
    @artwork = Artwork.find(params[:id])
  end
 
  def create
    @artwork = Artwork.new(artwork_params)
    if @artwork.save
      redirect_to @artwork
    else
      render 'new'
    end
  end

  def update
    @artwork = Artwork.find(params[:id])   
    if @artwork.update(artwork_params)
      redirect_to @artwork
    else
      render 'edit'
    end   
  end
 
private
  def artwork_params
    params.require(:artwork).permit(:artist, :title, :year, :about)
  end 
 
end

edit.html.erb:
<h1>Editing artwork</h1>
<%= render 'form' %>
<%= link_to 'Back', artworks_path %>
_form.html.erb:
<%= form_for :artwork do |f| %>
 
  <% if @artwork.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= pluralize(@artwork.errors.count, "error") %> prohibited
        this artwork from being saved:
      </h2>
      <ul>
        <% @artwork.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
 
  <p>
    <%= f.label :artist %><br>
    <%= f.text_field :artist %>
  </p>
 
  <p>
    <%= f.label :year %><br>
    <%= f.text_field :year %>
  </p>
 
  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </p>
 
  <p>
    <%= f.label :about %><br>
    <%= f.text_area :about %>
  </p>
 
  <p>
    <%= f.submit %>
  </p>
 
<% end %>

 

Ryan Bigg

unread,
Feb 27, 2016, 4:38:32 PM2/27/16
to rubyonra...@googlegroups.com
I think that should be form_for @artwork
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Documentation" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-do...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-docs.
For more options, visit https://groups.google.com/d/optout.

Garry Smith

unread,
Apr 29, 2016, 7:57:50 AM4/29/16
to Ruby on Rails: Documentation
Hi I am also doing this tutorial but go stuck on 5.12 Using Partials.
I kept getting a message saying that the template was missing, but it wasn't.
It kept looking for _form instead of _form.html.erb, not sure why.  So to get it working I had to change the file to _form, this doesn't seem correct. I have checked all the code and all is correct, to make sure I copied the code from the web page to eradicate any typos.
I did carry on with the tutorial which  I have know finished, but this is still bugging be for future refernce.
Reply all
Reply to author
Forward
0 new messages