-
Notifications
You must be signed in to change notification settings - Fork 0
16 Categories New
Dave Strus edited this page Jul 16, 2015
·
1 revision
Add the components necessary for creating new Categories.
Hint: You'll need a new controller, new routes, and at least one new view.
config/routes.rb
resources :categories
New file: app/controllers/categories_controller.rb
class CategoriesController < ApplicationController
def new
@category = Category.new
end
def create
@category = Category.new category_params
if @category.save
redirect_to posts_path, flash: { notice: 'Your category has been created.' }
else
flash.now[:error] = @category.errors.full_messages
render :new
end
end
private
def category_params
params.require(:category).permit(:name, :title, :description, :sidebar, :submission_text)
end
end
app/views/layouts/application.html.erb
<nav id="sidebar">
<%= link_to 'Submit a new link', new_post_path %>
<%= link_to 'Submit a new text post', new_post_path(post_type: :text) %>
<%= link_to 'Create a new category', new_category_path %>
</nav>
New directory: app/views/categories
New file: app/views/categories/new.html.erb
<%= form_for @category do |f| %>
<fieldset>
<%= f.label :name %>
<%= f.text_field :name %>
</fieldset>
<fieldset>
<%= f.label :title %>
<%= f.text_field :title %>
</fieldset>
<fieldset>
<%= f.label :description %>
<%= f.text_area :description %>
</fieldset>
<fieldset>
<%= f.label :sidebar %>
<%= f.text_area :sidebar %>
</fieldset>
<fieldset>
<%= f.label :submission_text %>
<%= f.text_area :submission_text %>
</fieldset>
<%= f.submit class: 'btn btn-default' %>
<% end %>
Try out the new form, and check the results in the Rails console. If it looks good, commit!
$ git add .
$ git commit -m "Create new categories."