2017-05-11 4 views
-1

Ich füge den Titel und die Beschreibung mit dem todo_list Formular, dann fügen Sie den Inhalt zum todo_item Formular. Aber ich möchte das Todo_list title-Feld im Titelfeld auf jeder Zeile, die ich todo_item erstelle, registrieren. Wie kann ich dies auf der Controllerseite bereitstellen?Rails todo Liste ausblenden erstellen Spalte

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 Modell

class TodoItem < ActiveRecord::Base 
belongs_to :todo_list 

def completed? 
    !completed_at.blank? 
end 
end 

todo_list Modell

class TodoList < ActiveRecord::Base 
    has_many :todo_items 
end 

todo_item db

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 

ToDo-Liste db

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

     t.timestamps 
    end 
    end 
end 

ToDo-Liste _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 %> 

todo Artikel _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 Artikel _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> 

Antwort

1

Dies ist, was Sie sind versuchen, ich denke, Fügen Sie dies in Ihrem ToDoItem Modell ..

before_create :set_title 

def set_title 
    self.title = todo_list.title 
end 

Hoffe, es hilft ..


aktualisieren, wie in den Kommentaren angefordert.

Aktualisieren von untergeordneten Elementen, wenn das übergeordnete Element aktualisiert wird.

In Ihrem TodoList Modell,

before_save :update_todo_item_titles 

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

danke Ihnen so sehr. es funktionierte. –

+0

Hallo Ferhan Todo Liste Titel Wenn ich meinen Titel bearbeite, wie ändere ich den Titel der Todo-Artikel? –

+0

Ändern Sie 'before_create' in' before_save'. –