0

每个项目可以有一个页面:

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

但是当我去保存一个页面时,我不仅得到一个路由错误,而且它实际上并没有保存到数据库中。

Routing Error

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

我的表单操作如下所示:

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

有谁知道发生了什么?我从其他 SO 帖子中将所有这些拼凑在一起,但是我在这里或那里更改的行越多,我觉得我离可行的解决方案越来越远。例如,如果我将表单操作更改为:

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

一切都神奇地起作用,但是编辑操作仍然被破坏。我在屠杀什么基本概念?

谢谢! - 标记

4

1 回答 1

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

于 2013-02-03T03:13:33.310 回答