开发者

rails not creating records - routes problem

my routes

TerritoryManagement::Application.routes.draw do

  get 'new' => 'territories#new', :as => 'new'

  root :to => 'territories#index', :as => 'territories'
  resources :territories
  resources :users
end

create in my controller

def create
  @territory = Territory.new(params[:territory])
  if @territory.save
    redirect_to root_url, :notice => "Product successfully created!"
  else
    render "new"
end

my view

<%= form_for(@territory) do |f| %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

this generates

<form accept-charset="UTF-8" action="/" clas开发者_如何学Cs="new_territory" id="new_territory" method="post">

I know that this action="/" is the problem, but I don't understand why it's being generated? How to modify my routes that the app will create the record and then goes to the index or edit view?

Thanks Thomas


This worked for me:

TerritoryManagement::Application.routes.draw do
   get 'new' => 'territories#new', :as => 'new'
   resources :territories
   root :to => 'territories#index'
end

It seems that the :as option was causing some issue. That's there to give the route a name, but since you've already done 'resources :territories' you already have named routes for the standard CRUD actions. I also moved the root route to the end of the file. I can't remember why, but it seems like this was a 'Best Practice' back in the Rails 2.3 days.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜