Rails简单形式路由问题

时间:2013-04-15 03:30:27

标签: ruby-on-rails ruby forms routing

我在Rails中有一个表单

<div class="page-header">
    <h3>Create Blah</h3>
</div>
<%= simple_form_for @blah do |f| %>
    <%= f.input :id %>
    <%= f.input :name %>
    <%= f.input :pho %>
    <%= f.input :fun %>
    <%= f.submit :class => 'btn btn-primary' %>
<% end %>
<br>

当我点击提交按钮时,代码会在哪里尝试?它是否为blah_controller.rb调用了create方法?因为目前我收到了路由错误

Routing Error
uninitialized constant BlahsController

以下是BlahController#create方法:

 def create
    authorize! :create, :blahs
    @blah = Blah.new(params[:blah])
    if @blah.save
      redirect_to admin_blah_path(@blah), :notice => 'New blah created!'
    else
      render :new
    end
 end

在我的rake routes中,我有

    admin_blahs GET    /admin/blahs(.:format)                      admin/blahs#index
                POST   /admin/blahs(.:format)                      admin/blahs#create
 new_admin_blah GET    /admin/blahs/new(.:format)                  admin/blahs#new
edit_admin_blah GET    /admin/blahs/:id/edit(.:format)             admin/blahs#edit
     admin_blah GET    /admin/blahs/:id(.:format)                  admin/blahs#show
                PUT    /admin/blahs/:id(.:format)                  admin/blahs#update
                DELETE /admin/blahs/:id(.:format)                  admin/blahs#destroy

1 个答案:

答案 0 :(得分:1)

看起来您的BlahsController是一个命名空间控制器,位于Admin模块下(即其完全限定名称为Admin::BlahsController)。如果是这样,在构造表单时,您还必须使用以下内容提供:admin命名空间:

<%= simple_form_for [:admin, @blah] do |f| %>

请参阅“处理命名空间”部分下的Rails Guide to Form Helpers