Spencer Hire
Spencer Hire

Reputation: 763

Rails edit routes

This error is creeping in on more than one occasion and i can't seem to pin point it. error log:

    undefined method `medium_path' for #<#<Class:0x0000010687a788>:0x00000101d62d90>
Extracted source (around line #3):

media controller.

 class MediaController < ApplicationController

    def index
        @medias = Media.all
    end

    def show
        @media = Media.find(params[:id])
    end

    def edit
        @media = Media.find(params[:id])
    end

end

edit.html.erb.

 <h1>Editing <%= @media.title %></h1>

<%= form_for(@media) do |f| %>
    <p>
        <%= f.label :title %>
    </p>

<% end %>

routes.rb

  Mediastuff::Application.routes.draw do

  root "media#index" 
  get "media" => "media#index"
  get "media/:id" => "media#show", as: "show_media"
  get "media/:id/edit" => "media#edit", as: "edit_media"



end

Upvotes: 2

Views: 5806

Answers (1)

vee
vee

Reputation: 38645

I believe that error is generated from your form_for declaration. In addition to what you already have in your config/routes.rb, you may also want to add a route for update action as that form_for(@media) is going to be an update.

Add the following to your config/routes.rb:

put "media/:id/update" => "media#update"

Also make sure to define update action in your MediaController.

Another option would be to use resources in config/routes.rb as a replacement to all the media/... routes you have:

Mediastuff::Application.routes.draw do
  root "media#index" 
  resources :media
end

And to see what path/url helpers you can use, run rake routes from terminal.

Upvotes: 5

Related Questions