开发者

How do I handle self joins with decent_exposure?

开发者 https://www.devze.com 2023-03-02 13:28 出处:网络
I have a Forum model, whose instances can have many nested forums: class Forum < ActiveRecord::Base

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,开发者_C百科 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号