0

在我的应用程序中,我有几个资源范围为“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

_表单代码:

<% 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 %>

我究竟做错了什么?

4

1 回答 1

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范围并正确生成 url,具体取决于是否@event保留在数据库中。

于 2012-09-11T19:21:37.340 回答