문제

I have a Forum model, whose instances can have many nested forums:

class Forum < ActiveRecord::Base
  has_many :nested_forums,
           :class_name => 'Forum',
           :foreign_key => 'parent_forum_id',
           :dependent => :nullify

  belongs_to :parent_forum,
             :class_name => 'Forum'
end

My routes go like this:

# Standard resource routes
GET /forums/:id                          =>  'forums#show'
# ...
GET  /forums/:forum_id/nested_forums/new =>  'Forums/nested_forums#new'
POST /forums/:forum_id/nested_forums     =>  'forums/nested_forums#create'

Now, in the controller:

class Forums::NestedForumsController < ApplicationController
  expose(:forum)                 # Will find with :forum_id
  expose(:nested_forums)         { forum.nested_forums }
  expose(:nested_forum)          # Main issue here
end

In the :create action, the code generated by the line expose(:nested_forum) will try to create a forum with params[:nested_forum]. This is an awkward situation since I'm using a self-join and there is no NestedForum model, and there doesn't seem to be a way to tell that to decent_exposure.

What would be a good way to handle this situation?

도움이 되었습니까?

해결책

In my form, I had:

form_for nested_forum, :url => { ... } do |f|
  # ...
end

I added the :as => :nested_forum argument to the form_for method call and the form now stores the parameters in params[:nested_forum], which solves my problem.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top