def card_params
params.require(:card).permit(:original_text, :translated_text, :review_date, :image, :deck_id, deck: [:title]).deep_merge(deck: {user_id: current_user.id})
end
def self.create_with_deck(params)
deck_params = params.delete(:deck)
if params[:deck_id].blank?
deck = Deck.create(deck_params)
params.deep_merge!(deck_id: deck[:id])
end
create(params)
end
class Task < ActiveRecord::Base
include AASM
aasm :column => 'status' do
state :in_work, :initial => true
state :completed
event :run do
transitions :from => :completed, :to => :in_work
end
event :complete do
transitions :from => :in_work, :to => :completed
end
end
class TasksController < ApplicationController
before_action :find_task, only: [:show, :edit, :update, :destroy, :run, :complete]
def run
@task.run!
redirect_to tasks_path
end
def complete
@task.complete!
redirect_to tasks_path
end
<% @tasks.each do |task| %>
<% if task.in_work? %>
<%= link_to 'Complete', complete_task_path(task), method: :put %>
<% else %>
<%= link_to 'Run', run_task_path(task), method: :put %>
<% end %>
<% end %>
resources :tasks do
member do
put :run
put :complete
end
end
def card_params
params.require(:card).permit(:original_text, :translated_text, :review_date, :image, :deck_id, deck: [:title])
end
<%= simple_form_for @card do |f| %>
<%= f.input :original_text %>
<%= f.input :translated_text %>
<%= f.input :image %>
<%= f.association :deck, label: "Выберите колоду" %>
<%= f.simple_fields_for @deck do |deck| %>
<%= deck.input :title, label: "Или создайте новую" %>
<% end %>
<%= f.input :review_date %>
<%= f.button :submit %>
<% end %>
class Task < ActiveRecord::Base
has_many :subtasks, class_name: 'Task', foreign_key: "parent_id"
belongs_to :parent, class_name: 'Task'
accepts_nested_attributes_for :subtasks, allow_destroy: true
belongs_to :user
belongs_to :project
end
def task_params
params.require(:task).permit(:title, :description, :priority, :status, :scheduled, :deadline, subtasks_attributes: [:title])
end
<%= simple_form_for @task do |t| %>
<%= t.simple_fields_for :subtasks, @task.subtasks.build do |f| %>
<div class="form-inputs">
<%= f.input :title %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
<% end %>