Guest User

Untitled

a guest
Jun 19th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. 1) class CreateTodos < ActiveRecord::Migration
  2. def change
  3. create_table :todos do |t|
  4. t.string :text
  5. t.boolean :isCompleted
  6. t.references :project, foreign_key: true
  7. t.timestamps
  8. end
  9. end
  10. end
  11.  
  12. 2) class CreateProjects < ActiveRecord::Migration[5.2]
  13. def change
  14. create_table :projects do |t|
  15. t.string :title
  16. t.timestamps
  17. end
  18. end
  19. end
  20.  
  21. Rails.application.routes.draw do
  22. root 'projects#index'
  23. resources :projects
  24. resources :todos
  25. end
  26.  
  27. <div class="form-box">
  28. <div class="form-box-wrapper">
  29. <%= form_with scope: :todo, url: todos_path, local: true do |form| %>
  30. <%= form.text_field :text, placeholder: 'Название задачи' %>
  31. <%= select(:post, :project_id, Project.all.collect{|p| [ p.title, p.id ] }, {include_blank: 'None'}) %>
  32. <br>
  33. <%= form.submit %>
  34. <a href="/" id="candel_button" class="cansel">ОТМЕНА</a>
  35. <% end %>
  36. </div>
  37. </div>
  38.  
  39. class Todo < ApplicationRecord
  40. belongs_to :project, optional: true
  41. end
  42.  
  43. class Project < ApplicationRecord
  44. has_many :todos
  45. end
  46.  
  47. def create
  48. @todo = Todo.new(todo_params)
  49. if @todo.save
  50. redirect_to @todo
  51. else
  52. render :edit
  53. end
  54. end
  55.  
  56. private
  57.  
  58. def todo_params
  59. params[:todo].permit(:text, :project_id)
  60. end
  61.  
  62. <div class="form-box">
  63. <div class="form-box-wrapper">
  64. <%= form_with model: @todo, local: true do |form| %>
  65. <%= form.text_field :text, placeholder: 'Название задачи' %>
  66. <%= form.select(:project_id, Project.all.collect{|p| [ p.title, p.id ] }, {include_blank: 'None'}) %>
  67. <br>
  68. <%= form.submit %>
  69. <a href="/" id="candel_button" class="cansel">ОТМЕНА</a>
  70. <% end %>
  71. </div>
Add Comment
Please, Sign In to add comment