ItsJoeTurner
ItsJoeTurner

Reputation: 603

Creating automatically with belongs_to in Rails

My project has a Brand model and a Project model. The projects belong_to the brand and the Brand has_many projects. Currently when creating a new project I have to assign it to a brans by using associations in Simple Form.

I'm wondering if there's a way to automatically to create a project inside the brand, so there is no need to select from a list of brands every single time you create a project.

routes.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

Brand

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

Project

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

New Project Form

<%= 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 %>

Displaying The Brands

<% @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>

Important Routes

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

Upvotes: 0

Views: 203

Answers (1)

hiattp
hiattp

Reputation: 2336

This should be pretty straight forward if you set up your routes properly:

resources :brands do
  resources :projects
end

Then your form would look something like:

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

where @project = Project.new. This will automatically set the :brand_id to @brand.id in your resulting post params.

Upvotes: 1

Related Questions