Question

I have a model, Report, that is polymorphic. So many itens in my site may have many of it.

And i would like to have a generic controller for posting it. Its a very simple model, has only a text message and the association.

in my routes, im doing something like

map.resources :users, :has_many => [ :reports ]
map.resources :posts, :has_many => [ :reports ]

but in my reports_controller, i would like to get the relation from with its coming from.

like:

before_filter :get_reportable

def get_reportable
   reportable = *reportable_class*.find params[:reportable_id]
end

is this possible?

how could i get the reportable_class and the reportable_id?

I can get the params[:user_id] when it comes from users controller, or params[:post_id] when it comes from posts. I could do a case with all the relations, but it doesnt seem a clean solution at all...

having the polymorphic association would be the best, are there any how?

Was it helpful?

Solution

If you have a single controller that processes requests through two differing paths, then you need to make it aware of the contexts in which it will be called. You often see a lot of code that looks something like this:

before_filter :load_reportable

def load_reportable
  if (params[:user_id])
    @user = User.find(params[:user_id])
    @reportable = @user
  elsif (params[:post_id])
    @post = Post.find(params[:post_id])
    @reportable = @post
  end
rescue ActiveRecord::RecordNotFound
  render(:partial => 'not_found', :status => :not_found)
  return false
end

Since you're using a polymorphic association, you may be able to do something like this instead:

before_filter :load_reportable

def load_reportable
  unless (@reportable = @report.reportable)
    # No parent record found
    render(:partial => 'not_found', :status => :not_found)
    return false
  end

  # Verify that the reportable relationship is expressed properly
  # in the path.

  if (params[:user_id])
    unless (@reportable.to_param == params[:user_id])
      render(:partial => 'user_not_found', :status => :not_found)
      return false
    end
  elsif (params[:post_id])
    unless (@reportable.to_param == params[:post_id])
      render(:partial => 'post_not_found', :status => :not_found)
      return false
    end
  end
end

The trouble with this approach, where you have one controller that serves two entirely different routes, is that generating error messages, such as "user not found" versus "post not found". This can be tricky to get right if you're not inheriting from a Users::BaseController, for instance.

In many cases it's easier to create two independent "reports" controllers, such as users/reports and posts/reports, where any common functionality is imported from a module. These controllers usually inherit from a base controller which performs the loading and error handling. The base controller can also establish layout, page title, etc., without having to re-implement this functionality for each sub-resources controller.

The alternative is to de-couple reports and have it run as its own controller where the relationship to the "reportable" record is mostly irrelevant.

OTHER TIPS

Or try that:

before_filter :get_reportable

def get_reportable
  params.each do |name, value|
    if name =~ /(.+)_id$/
      @reportable = $1.classify.constantize.find(value)
    end
  end
end

It is going through all the params and tries to find one ending with _id, then grabs that before part and finds relevant record.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top