0

我的项目有一个品牌模型和一个项目模型。项目属于_品牌,品牌有_许多项目。目前,在创建一个新项目时,我必须通过使用简单形式的关联将其分配给一个麸皮。

我想知道是否有一种方法可以在品牌内自动创建项目,因此每次创建项目时都无需从品牌列表中进行选择。

路线.rb

IdeaBook::Application.routes.draw do

devise_for :users

  resources :agencies


  resources :brands do
    resources :projects do
      resources :ideas
    end
  end

root :to => "brands#index"

devise_scope :user do
  get "/login" => "devise/sessions#new"
end

class Brand < ActiveRecord::Base
  attr_accessible :description, :brand_id, :name
  has_many :projects
  belongs_to :agency
end

项目

class Project < ActiveRecord::Base
  attr_accessible :description, :project_id, :brand_id, :name, :project_end, :project_start, :url
  has_many :ideas
  belongs_to :brand
end

新项目表格

<%= simple_form_for ([@brand, @new_project]) do |f| %>

<% if @project.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>

      <ul>
      <% @project.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
<% end %>

  <%= f.input :name %>
  <%= f.input :description %>
  <%= f.input :url %>
  <%= f.input :project_start %>
  <%= f.input :project_end %>
  <%= f.button :submit, :class => "btn btn-success" %>
<% end %>

展示品牌

<% @brand.projects.each do |project| %>

<div class="span4">
<div class="media idea">

    <a class="pull-left" href="#">
      <img class="media-object" src="" alt="">
    </a>

    <div class="media-body">
      <h4 class="media-heading title"><%= link_to project.name, project %></h4>
      <p class="desc"><%= project.description %></p>
    </div>

</div>
</div>

<% end %>

<div class="span4">

  <div class="new">
    <%= link_to 'Create New Project', new_project_path %>
  </div>

</div>

重要路线

brand_projects GET    /brands/:brand_id/projects(.:format)          projects#index
                         POST   /brands/:brand_id/projects(.:format)          projects#create
       new_brand_project GET    /brands/:brand_id/projects/new(.:format)      projects#new
      edit_brand_project GET    /brands/:brand_id/projects/:id/edit(.:format) projects#edit
           brand_project GET    /brands/:brand_id/projects/:id(.:format)      projects#show
                         PUT    /brands/:brand_id/projects/:id(.:format)      projects#update
                         DELETE /brands/:brand_id/projects/:id(.:format)      projects#destroy
                  brands GET    /brands(.:format)                             brands#index
                         POST   /brands(.:format)                             brands#create
               new_brand GET    /brands/new(.:format)                         brands#new
              edit_brand GET    /brands/:id/edit(.:format)                    brands#edit
                   brand GET    /brands/:id(.:format)                         brands#show
                         PUT    /brands/:id(.:format)                         brands#update
                         DELETE /brands/:id(.:format)                         brands#destroy
4

1 回答 1

1

如果您正确设置路线,这应该非常简单:

resources :brands do
  resources :projects
end

然后你的表格看起来像:

<%= form_for([@brand, @project]) do |f| %>
    ...form fields...
<% end %>

哪里@project = Project.new。这将在您生成的帖子参数中自动设置:brand_id为。@brand.id

于 2013-09-06T13:03:38.597 回答