为创建操作创建的Rails 4嵌套资源错误

时间:2014-01-23 05:07:52

标签: ruby-on-rails ruby ruby-on-rails-4

我有以下路线:

resources :venues, shallow: true do
        #Halls
        get "hall/:id/exhibition" => "halls#exhibition", as: :exhibition
        get "hall/:id/visit" => "halls#visit", as: :hall_visit
        get "structure", :to => "venues#venue_structure"
        resources :asset_types, :booths_tags, :tags, :uploaded_files, :events, :chats
        resources :halls do
            resources :webcasts
            resources :booths do
                resources :chats
            end
        end
    end

创建行动:

 # POST /halls
  def create
    @hall = Hall.new(hall_params)

    if @hall.save
      redirect_to @hall, notice: 'Hall was successfully created.'
    else
      render action: 'new'
    end 
  end

但是我现在因此错误:

undefined method `halls_path' for #<#<Class:0xb0c7300>:0xab9e7c0>

有没有办法让这个render action: 'new'部分重定向回形成验证和错误消息?

耙路线

venue_halls_path     GET     /venues/:venue_id/halls(.:format)   halls#index
                     POST    /venues/:venue_id/halls(.:format)   halls#create
new_venue_hall_path  GET     /venues/:venue_id/halls/new(.:format)   halls#new
edit_hall_path       GET     /halls/:id/edit(.:format)   halls#edit
hall_path            GET     /halls/:id(.:format)    halls#show
                     PATCH   /halls/:id(.:format)    halls#update
                     PUT     /halls/:id(.:format)    halls#update
                     DELETE  /halls/:id(.:format)    halls#destroy

1 个答案:

答案 0 :(得分:2)

当您在Rails中嵌套这样的东西时,当您想要转到该对象时,您不能只是重定向到该对象。您还必须包含:venue

@venue = params[:venue] #or however you can get the venue
redirect_to [@venue, @hall], notice: 'Hall was successfully created.'

请注意,对象的路径现在是一个数组,因为它的嵌套方式。

试试......让我知道它对你有用。

另外......阅读本文 - http://guides.rubyonrails.org/routing.html#nested-resources - 阅读整篇文章,但这是关于嵌套的部分