Hello. I'm currently building a bulletin board in Rails.
I wanted to classify the threads on the bulletin board by category and display them, so I implemented it with the intention of selecting the category when creating threads.
I've started to touch Rails recently, so if you are familiar with it, please ask Masakari.
Ruby 2.5 Rails 5.1
Designed with many threads tied to a category
Don't forget to set between models ...
thread.rb
class Thread < ApplicationRecord
belongs_to :category
end
category.rb
class Category < ApplicationRecord
has_many :threads
end
It won't start without a category, so let's register. You can use it from the console, but I think I'll add more categories in the future, so I'll create a registration form. After registering, you will be taken to the list page.
First, write routes and controller
--Routing
routes.rb
Rails.application.routes.draw do
root 'thread#index'
resources :thread #I also write thread routing
resources :categories
end
--Controller
ruby:categories.controller.rb
class CategoriesController < ApplicationController
def new
@category = Category.new
end
def create
@category = Category.new(category_params)
if @category.save
redirect_to categories_path, notice: "Has registered"
else
render :new
end
end
def index
@categories = Category.all
end
private
def category_params
params.require(:category).permit(:name)
end
end
ruby:new.html.erb
<div class="col-sm-12">
<h2 class="text-center">Add category</h2>
<%= form_with model: @category, local: true do |f| %>
<div class="form_input">
<%= f.label :name %>
<%= f.text_field :name, class:"form-control" %>
</div>
<div class="form_action row">
<%= f.submit "sign up", class: "btn col-sm-12 submit_btn" %>
</div>
<% end %>
</div>
--List page
I wish I could display this for the time being
index.rb
<div>
<% @categories.each do |category| %>
<%= category.name %>
<% end %>
</div>
Now that you've registered, you'll be able to select a category on the thread's post form.
--Controller
threads_controller.rb
class ThreadsController < ApplicationController
def new
@thread = Thread.new
end
def create
@Thread = Thread.new(board_params)
if @thread.save
redirect_to thread_path(@thread), notice: "Post has been completed"
else
render :new
end
end
def show
@thread = Thread.find(params[:id])
end
private
def board_params
params.require(:thread).permit(:title,:body)
end
end
--Posting page
You can create a selection form with collection_select
.
As for how to use it, I use it like this ...
collection_select (object name, method name, array of elements, value attribute item, text item [, option or HTML attribute or event attribute])
In this example, Category.all
corresponds to the" array of elements "part.
Perhaps, rather than writing Category.all
, put it in a variable on the controller and use it in view. It may be better to have a shape like that.
It would be helpful if someone could point out here.
ruby:threads/new.index.erb
<div class="col-sm-12">
<h2 class="text-center">Make a thread</h2>
<%= form_with model: @thread, local: true do |f| %>
<div class="form_input">
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
</div>
<div class="form_input">
<%= f.label :body %>
<%= f.text_area :body, class: "form-control" %>
</div>
<div class="form_input">
<%= f.label :category_id %>
<%= f.collection_select :category_id, Category.all, :id, :name,
:include_blank => "Please select a category" %>
</div>
<div class="form_action row">
<%= f.submit "Post", class: "btn col-sm-12 submit_btn" %>
</div>
<% end %>
</div>
You will be able to select as follows.
Thank you for watching until the end. I will write a lot of articles from now on, and I will output more and more.