module Orchestrate::Rails::Tutorial::TheCreateAction

Adding the create action ↑

We'll now implement the ability to add new films to the collection.

Updating the films controller. ↑

In this case we'll add two new actions to the controller:

Update <rails-root>/app/controllers/films_controller.rb to add the new and create actions, along with the private methods attributes and generate_primary_key.

The rails application is responsible for setting the value of the id attribute when updating the collection with newly created data. In this example, we'll use generate_primary_key to derive the primary_key from the film title, subsequently using the value to set the id attribute.

class FilmsController < ApplicationController

  def index
    @films = Film.all
  end

  def show
    @film = Film.find params[:id]
    @comments = @film.comments
    @sequels = @film.sequels
    @spawn_of_sequels = @film.spawn_of_sequels
  end

  def new
    @film = Film.new
  end

  def create
    @film = Film.create! attributes
    redirect_to :action => 'show', :id => @film.id
  end

  private
    # "The Godfather:Part IV" --> "the_godfather_part_iv"
    def generate_primary_key
      params[:film][:title].downcase.tr(': ', '_')
    end

    def attributes
      params[:film].merge(:id => generate_primary_key)
    end
end

Creating the view for the new action. ↑

Let's add a form to enter a new film into the collection.

Reminder: HAML is quite particular about indentation, so take care to keep the code aligned properly when pasting into your editor.

Create <rails-root>/app/views/films/new.html.haml as shown:

.row
 = form_for(@film, url: { action: 'create'}, html: { class: "form-signin"}) do |f|
  %h2="Add a new movie!"
  = f.text_field :title,    class: "form-control text-top",    placeholder: "Title"
  = f.text_field :year,     class: "form-control text-middle", placeholder: "Year"
  = f.text_field :rated,    class: "form-control text-middle", placeholder: "Rated"
  = f.text_field :genre,    class: "form-control text-middle", placeholder: "Genre"
  = f.text_field :director, class: "form-control text-middle", placeholder: "Director"
  = f.text_field :writer,   class: "form-control text-middle", placeholder: "Writer"
  = f.text_field :actors,   class: "form-control text-middle", placeholder: "Actors"
  = f.text_field :type,     class: "form-control text-bottom", placeholder: "Type"
  = f.submit "Add my movie", class: "btn btn-lg btn-primary btn-block"

Adding a form-specific stylesheet ↑

We'll need to add a new stylesheet for the form we just created.

Create <rails-root>/app/assets/stylesheets/form-signin.css as shown:

.form-signin {
  max-width: 550px;
  padding: 15px;
  margin: 0 auto;
}

.form-signin .form-signin-heading,
.form-signin .checkbox {
  margin-bottom: 10px;
}

.form-signin .checkbox {
  font-weight: normal;
}

.form-signin .form-control {
  position: relative;
  font-size: 16px;
  height: auto;
  padding: 10px;
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box;
}

.form-signin .form-control:focus {
  z-index: 2;
}

.form-signin input.text-top {
  margin-bottom: -1px;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

.form-signin input.text-middle {
  margin-bottom: -1px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

.form-signin input.text-bottom {
  margin-bottom: 10px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

.form-signin .input-group {
  width: 100%;
}

.form-signin .input-group-addon {
  background-color: #bbb;
}

Check it out ↑

Navigate to films#new and add a new movie!

Next ↑

Adding search capability