2017-05-11 107 views
-1

我使用todo_list表单添加标题和说明,然后将内容添加到todo_item表单中。但我想在创建todo_item的每一行的标题字段中注册todo_list标题字段。我如何在控制器端提供这个功能?Rails待办事项列表隐藏创建列

class TodoItemsController < ApplicationController 
    before_action :set_todo_list 
    before_action :set_todo_item, except: [:create] 

    def create 
     @todo_item = @todo_list.todo_items.create(todo_item_params) 

     redirect_to @todo_list 
    end 

end 

todo_item模型

class TodoItem < ActiveRecord::Base 
belongs_to :todo_list 

def completed? 
    !completed_at.blank? 
end 
end 

todo_list模型

class TodoList < ActiveRecord::Base 
    has_many :todo_items 
end 

todo_item分贝

class CreateTodoItems < ActiveRecord::Migration 
    def change 
    create_table :todo_items do |t| 
    t.string :title 
    t.string :content 
    t.references :todo_list, index: true 

    t.timestamps 
    end 
    end 
end 

待办事项列表DB

class CreateTodoLists < ActiveRecord::Migration 
    def change 
    create_table :todo_lists do |t| 
     t.string :title 
     t.text :description 

     t.timestamps 
    end 
    end 
end 

待办事项列表_form.html.erb

<%= form_for(@todo_list) do |f| %> 
    <% if @todo_list.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@todo_list.errors.count, "error") %> prohibited 
this todo_list from being saved:</h2> 

     <ul> 
     <% @todo_list.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :title %><br> 
    <%= f.text_field :title %> 
    </div> 
    <div class="field"> 
    <%= f.label :description %><br> 
    <%= f.text_area :description %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
    <% end %> 

待办事项_form.html.erb

<%= form_for([@todo_list, @todo_list.todo_items.build]) do |f| %> 
    <%= f.text_field :content, placeholder: "New Todo" %> 
    <%= f.submit %> 
<% end %> 

待办事项_todo_item_html.erb

<div class="row clearfix"> 
    <% if todo_item.completed? %> 
     <div class="complete"> 
      <%= link_to complete_todo_list_todo_item_path(@todo_list, 
todo_item.id), method: :patch do %> 
       <i style="opacity: 0.4;" class="fa fa-check"></i> 
      <% end %> 
     </div> 
     div class="todo_item"> 
      <p style="opacity: 0.4;"><strike><%= todo_item.content %> 
</strike></p> 
     </div> 
     <div class="trash"> 
      <%= link_to todo_list_todo_item_path(@todo_list, todo_item.id), 
method: :delete, data: { confirm: "Are you sure?" } do %> 
      <i class="fa fa-trash"></i> 
      <% end %> 
     </div> 
    <% else %> 
     <div class="complete"> 
      <%= link_to complete_todo_list_todo_item_path(@todo_list, 
todo_item.id), method: :patch do %> 
       <i class="fa fa-check"></i> 
      <% end %> 
     </div> 
     <div class="todo_item"> 
      <p><%= todo_item.content %></p> 
     </div> 
     <div class="trash"> 
      <%= link_to todo_list_todo_item_path(@todo_list, todo_item.id), 
method: :delete, data: { confirm: "Are you sure?" } do %> 
       <i class="fa fa-trash"></i> 
      <% end %> 
     </div> 
    <% end %> 
</div> 

回答

1

这是你是什么试图做我猜, 在您的ToDoItem模型中加入此..

before_create :set_title 

def set_title 
    self.title = todo_list.title 
end 

希望它可以帮助..


更新,在意见中的要求。

在更新父代时更新子代。

在你TodoList模型,

before_save :update_todo_item_titles 

def update_todo_item_titles 
    todo_items.update_all(title: title) if title_changed? 
end 
+0

太感谢你了。有效。 –

+0

你好 Ferhan todo list title当我编辑我的标题时,如何更改待办事项的标题? –

+0

将'before_create'改为'before_save'。 –