Add a new chirp

URI Pattern Controller#Action What should it do? Example action code
/chirps/new(.:format) chirps#new Show a form on a page for someone to fill out a new chirp Chirp.new
/chirps(.:format) chirps#create Create a new chirp that get's saved Chirp.create(body: 'a body', author: 'some author')

We'll start by making a form on http://localhost:3000/chirps/new. In the new action/function in app/controllers/chirps_controller.rb, we will add:

    @chirp = Chirp.new

And in app/views/chirps/new.html.erb, let's make a form:

<h1>Make a Chirp</h1>

<%= form_for @chirp do |f| %>

  <div>
    <%= f.label :author %>
    <%= f.text_field :author %>
  </div>

  <div>
    <%= f.label :body %>
    <%= f.text_area :body %>
  </div>

  <div>
    <%= f.submit 'Chirp!' %>
  </div>

<% end %>

Now, if we go to http://localhost:3000/chirps/new, we'll see a form for making a new chirp.

Let's try to chirp by filling out the form and pressing "Chirp!"

What happened? Discuss with the coach and look at the code the form_for function made for us in our HTML using Inspect Element.

Since submitting the form goes to the chirps#create controller action, chirps#create should take the parameters from the form and make a new Chirp with that.

Let's add the following to our app/controllers/chirps_controller.rb:

  def create
    @chirp = Chirp.new(params[:chirp].permit(:author, :body))
    if @chirp.save
      redirect_to chirp_path(@chirp)
    else
      render 'new'
    end
  end

Our controller should now look like this:

Now, when we submit...

we get redirected to the new chirp.

Review with your coach. What does the .permit in our code help do? What did we work with?

For convenience, let's add a link to adding a new chirp on http://localhost:3000/chirps.

We can do this by adding to app/views/chirps/index.html.erb, just below the heading:

<%= link_to "Make a new chirp", new_chirp_path %>
<hr>

http://localhost:3000/chirps will now have a link to make new chirps.