Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Rails Routes and Resources Routes to Update Actions An Update Action

Kian Chakamian
Kian Chakamian
15,410 Points

I am anticipating a patch request but am receiving a post instead

I am submitting the form on my edit.html.erb and it is giving me an error for No route matches [POST] "/grouponepostings/17/edit". I am using simple forms in my document and am not sure how to specify that it is a patch request I seek.

Here is routes.rb:

Rails.application.routes.draw do
  get "/", to: "pages#index"
  get "/grouponepostings", to:"grouponepostings#index"
  get '/grouponepostings/new', to: 'grouponepostings#new'
  get "/grouponepostings/:id", to:"grouponepostings#show"
  post '/grouponepages', to: 'grouponepostings#create'
  get '/grouponepostings/:id/edit', to: 'grouponepostings#edit'
  patch 'grouponepostings/:id/edit', to: 'grouponepostings#update'
end

Here is edit.html.erb:

<%= simple_form_for :postings1314 do |r| %>
  <div>
    <%=r.label :firstName%>
    <%=r.text_field :firstName%>
  </div>

  <div>
    <%=r.label :lastName%>
    <%=r.text_field :lastName%>
  </div>

  <div>
    <%=r.label :age%>
    <%=r.text_field :age%>
  </div>

  <div>
    <%=r.label :bio%>
    <%=r.text_field :bio%>
  </div>

  <div>
    <%= r.submit %>
  </div>
<%end%>

And here is grouponepostings_controller.rb:

class GrouponepostingsController < ApplicationController
  def index
    @postings1314 = Grouponepage.all
  end
  def show
    @posting1314singular = Grouponepage.find(params[:id])
  end
  def new
    @postings1314 = Grouponepage.new
  end
  def create
    page_params = params.require(:grouponepage).permit(:firstName, :lastName, :age, :bio)
    @posting1314 = Grouponepage.new(page_params)
    @posting1314.save
    redirect_to '/grouponepostings'
  end
  def edit
    @posting1314 = Grouponepage.find(params[:id])
  end
  def update
    @posting1314 = Grouponepage.find(params[:id])
    page_params = params.require(:grouponepage).permit(:firstName, :lastName, :age, :bio)
    @posting1314.update(page_params)
    redirect_to '/'
  end
end

1 Answer

Trevor J
seal-mask
.a{fill-rule:evenodd;}techdegree
Trevor J
Python Web Development Techdegree Student 2,107 Points

In your edit.html.erb

# Change this to @postings1314 for instance method or postings1314 for local method.  
# I have always used instance methods in my views but I hear local methods are recomended.
# Simple form docs say @postings1314
<%= simple_form_for :postings1314 do |r| %>

Also in your routes.rb

# Change this 
patch 'grouponepostings/:id/edit', to: 'grouponepostings#update'  
 # To this
patch '/grouponepostings/:id', to: 'grouponepostings#update'  

You can type rake routes in the terminal to see the routing table to help identify the URL path

Also in your create method I would make some changes.

# instead of 
def update
    @posting1314 = Grouponepage.find(params[:id])
    page_params = params.require(:grouponepage).permit(:firstName, :lastName, :age, :bio)
    @posting1314.update(page_params)
    redirect_to '/'
end

# I would suggest
def update
    @posting1314 = Grouponepage.find(params[:id])
    if @posting1314.update_attributes(page_params)
        redirect_to '/'
    else
      render 'edit'
    end
end

For this way to work you would need to do what I suggested in the last question and move page_params

 private
    def page_params
      page_params = params.require(:grouponepage).permit(:firstName, :lastName, :age, :bio)
    end
Kian Chakamian
Kian Chakamian
15,410 Points

Thanks for your help... again??. That worked as well.