PUT路由按范围划分

时间:2012-09-11 19:10:05

标签: ruby-on-rails-3

在我的应用程序中,我有几个限定为'admin'的资源,如下所示(在routes.rb中):

scope 'admin', :as => 'admin' do
    resources :events
end

因此,当我通过表单使用#new或#update方法时,我最终得到的ActionController异常为No route matches [PUT] "/admin/events"

rake routes产生以下内容:

        admin_events GET    /admin/events(.:format)             events#index
                     POST   /admin/events(.:format)             events#create
     new_admin_event GET    /admin/events/new(.:format)         events#new
    edit_admin_event GET    /admin/events/:id/edit(.:format)    events#edit
         admin_event GET    /admin/events/:id(.:format)         events#show
                     PUT    /admin/events/:id(.:format)         events#update
                     DELETE /admin/events/:id(.:format)         events#destroy

_form code:

<% if notice %>
  <p id="notice"><%= notice %></p>
<% end %>

<%= form_for @event, :html => { :class => 'form-horizontal' }, :url => url_for(:controller => 'events', :action => 'index') do |f| %>
<fieldset>

  <div class="control-group">
    <%= f.label :name, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :name %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :event_date, :class => 'control-label' %>
    <div class="controls date-selects">
      <%= f.datetime_select :event_date, :start_year => 2012 %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :publish_date, :class => 'control-label' %>
    <div class="controls date-selects">
      <%= f.datetime_select :publish_date, :start_year => 2012 %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :blurb, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_area :blurb, :class => 'span6 wysi' %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :graphic, :class => 'control-label' %>
    <div class="controls">
      <%= f.file_field :graphic %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :tix_link, :class => 'control-label' %>
    <div class="controls">
      <%= f.url_field :tix_link %>
    </div>
  </div>

  <div class="form-actions">
    <%= f.submit 'Submit', :class => 'btn-primary' %>
    <%= link_to t('.cancel', :default => t("helpers.links.cancel")), admin_events_path, :class => 'btn' %>
  </div>
</fieldset>
<% end %>

我做错了什么?

1 个答案:

答案 0 :(得分:1)

尝试更改表单标记:

<%= form_for @event, :html => { :class => 'form-horizontal' }, :url => url_for(:controller => 'events', :action => 'index') do |f| %>

为:

<%= form_for [:admin, @event], :html => { :class => 'form-horizontal' } do |f| %>

这将使用正确的admin范围并正确生成网址,具体取决于数据库中是否保留@event