Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1) class CreateTodos < ActiveRecord::Migration
- def change
- create_table :todos do |t|
- t.string :text
- t.boolean :isCompleted
- t.references :project, foreign_key: true
- t.timestamps
- end
- end
- end
- 2) class CreateProjects < ActiveRecord::Migration[5.2]
- def change
- create_table :projects do |t|
- t.string :title
- t.timestamps
- end
- end
- end
- Rails.application.routes.draw do
- root 'projects#index'
- resources :projects
- resources :todos
- end
- <div class="form-box">
- <div class="form-box-wrapper">
- <%= form_with scope: :todo, url: todos_path, local: true do |form| %>
- <%= form.text_field :text, placeholder: 'Название задачи' %>
- <%= select(:post, :project_id, Project.all.collect{|p| [ p.title, p.id ] }, {include_blank: 'None'}) %>
- <br>
- <%= form.submit %>
- <a href="/" id="candel_button" class="cansel">ОТМЕНА</a>
- <% end %>
- </div>
- </div>
- class Todo < ApplicationRecord
- belongs_to :project, optional: true
- end
- class Project < ApplicationRecord
- has_many :todos
- end
- def create
- @todo = Todo.new(todo_params)
- if @todo.save
- redirect_to @todo
- else
- render :edit
- end
- end
- private
- def todo_params
- params[:todo].permit(:text, :project_id)
- end
- <div class="form-box">
- <div class="form-box-wrapper">
- <%= form_with model: @todo, local: true do |form| %>
- <%= form.text_field :text, placeholder: 'Название задачи' %>
- <%= form.select(:project_id, Project.all.collect{|p| [ p.title, p.id ] }, {include_blank: 'None'}) %>
- <br>
- <%= form.submit %>
- <a href="/" id="candel_button" class="cansel">ОТМЕНА</a>
- <% end %>
- </div>
Add Comment
Please, Sign In to add comment