Question

I am using STI in my rails app:

class Project < ActiveRecord::Base
end

class Video < Project
end

I've got the following routes:

resources :projects
resources :videos, :controller => "projects", :type => "video"

And when I rake my routes I see that I should be able to POST to /videos:

POST   /videos(.:format)   projects#create {:type=>"video"}

However, when I visit /videos/new, I notice that the form posts to /projects

= form_for(@project) do |f|

... creates the following HTML ...

<form action="/projects" method="post" >
  <!-- ommited -->
</form

My new action in projects_controller looks like this:

def new
  @project = params[:type].capitalize.constantize.new
end

def create 
  @project = params[:type].capitalize.contstantize.new(project_params)
end

I want it to post to /videos and not /projects, because params[:type] is always set to "video" when we are on urls that start with /videos, whereas it's not set to anyything when we are on urls that start with /projects.

UPDATE:

I have a temporary fix:

= f.hidden_field :type

Does the trick when url changes to /projects, but I'd rather have the form post to /videos...

Was it helpful?

Solution 3

I've flagged the question for deletion because I had the following in my code:

def self.inherited(child)
  child.instance_eval do
    def model_name
      Project.model_name
    end
  end
  super
end

After removing that, everything works just the way I want it to. Whoops. Sorry!

OTHER TIPS

I will prefer to keep the routes like

resources :projects
resources :videos

and make a new controller for videos like

class VideosController < ProjectsController
    #actions for Video
end

The form_for tag allows you to change the URL which you POST to by setting the url option like so:

form_for(@project, :url => "/videos")

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