0

Each project can have a single page:

resources :project do
    resource :page
  end

  class Project < ActiveRecord::Base
    has_one :page
  end

  class Page < ActiveRecord::Base
    belongs_to :project
  end


  def new
    @project = Project.find(params[:project_id])
    @page = @project.build_page
      respond_to do |format|
        format.html
      end
    end

    def create
      @project = Project.find(params[:project_id])
      @page = @project.build_page(params[:page_id])

      respond_to do |format|
        if @page.save
          format.html { redirect_to @page, :notice => 'Page was successfully created.' }
        else
          format.html { render action: "new" }
        end
      end
    end

But when I go to save a page, I not only get a routing error, but it doesn't actually save to the db.

Routing Error

No route matches [POST] "/projects/2/pages"

My form action looks like this:

<%= form_for([@job, @page]) do |f| %>

Does anyone have any idea of what is going on? I kind of pieced all of this together from other SO posts, but the more I change a line here or there, I feel like I'm getting further from a working solution. For example, if I change the form action to be:

<%= form_for @page, url: job_page_path(@job) do |f| %>

Everything magically works, but then the edit action is still broken. What basic concept am I butchering?

Thanks! --Mark

Mark
  • 823
  • 2
  • 10
  • 16

1 Answers1

0

you have a typo:

 resource :page

should be

 resources :page

(notice the s)

resource (singular) is actually quite a different method that builds a different set of routes. See the docs for more info.

UPDATE / ERRATUM

sorry, i've read your question too fast. you should take a look at Ruby on rails: singular resource and form_for - it seems that form_for does not know how to properly handle singular resources.

Someone here on SO suggests a quick fix for this : nested form_for singular resource

Community
  • 1
  • 1
m_x
  • 12,357
  • 7
  • 46
  • 60