Melbourne2991
Melbourne2991

Reputation: 11797

Resource Route linked to incorrect action

I am just starting out with rails and have run into some difficulties. I am trying to create a basic form that will add a new entry to the "Main" database. When I submit the form, instead of running "new" it appears to be trying to run "update", which according to the documentation should be called by /photos/:id

There error I am getting on the browser is

Unknown action

The action 'update' could not be found for AdminController

Controller:

class AdminController < ApplicationController
    def index
    @post = Main.create
    end

    def new

    end
end

index.erb.html:

<%= form_for @post, :url => { :action => "new" }, :html => {:class => "nifty_form"} do |f| %>
  <%= f.text_field :title %>
  <%= f.text_area :entry, :size => "60x12" %>
  <%= f.submit "New" %>
<% end %>

Routes:

Tasks::Application.routes.draw do
   root :to => "Main#index"

   resources :main
   resources :admin

Upvotes: 0

Views: 33

Answers (1)

Martin M
Martin M

Reputation: 8648

In Rails new is supposed to show the form to enter a new item.
edit is for showing the form to edit an existing item.

The form data is then POSTed to mains_url if it's a new item or PUT if its an exitsing item.
POST is routed to the create action.
PUT is routed to the update action.

So, to create an item, you have to implement create, to update it, you have to implement update

see

rake routes

Upvotes: 2

Related Questions