[RUBY] How to easily create a pull-down in Rails

How to make a pull-down easily

Directory to use (in my case) ⚠️ Name yourself

Completed form to aim for


First from the view file (anywhere)

1. Such a pattern


          <option>Please select a user to chat with</option>
          <option>User 1</option>
          <option>User 2</option>

2. There is also such a pattern


  <div class="mentors-detail">
   <div class="form">
     <div class="weight-bold-text">
       <span class="indispensable">Mandatory</span>
     <%= f.collection_select(:skill_id, Skill.all, :id, :name, {}, {class:"select-box", id:"mentor-skill"}) %>

This time we will use 2 f.collection_select </ font> to create a pull-down!

The 6th line says ": skill_id, Skill.all", but this is the content of the pull-down you want to select is selected from "skill_id model" </ font>, so next is the contents of the model I will write!



class Skill < ActiveHash::Base
  self.data = [
    { id: 1, name: '--' },
    { id: 2, name: 'HTML•CSS' },
    { id: 3, name: 'Ruby' },
    { id: 4, name: 'Javascript' },
    { id: 5, name: 'PHP' },
    { id: 6, name: 'Python' },
    { id: 7, name: 'SQL' },
    { id: 8, name: 'GO' },

After that, write a new action or create action in the controller and write it while checking the routing and path with rails routes and it's done!



I've omitted the CSS part, but maybe it looks like this (try it yourself)


.mentors-detail {
  display: flex;
  justify-content: space-between;
  padding: 2vh 0;

.mentors-detail>.form {
  width: 300px;
  padding: 2vh 0;

.select-box {
  margin: 2vh 0;

That's all from the scene!

Recommended Posts