2017-05-11 30 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'。 –