2016-11-02 2 views
2

Ich habe ein kleines Problem, ich hoffe, etwas Hilfe zu bekommen.Schienen bauen Methode bricht Objekt-ID?

Ich habe einen Block und ein Formular auf der gleichen Seite positioniert, der gut arbeitet, wenn das Formular unterhalb dem Block positioniert ist:

<% @project.tasks.each do |task| %> 
    <%= link_to task.title, project_task_path(@project, task) %> 
<% end %> 

<%= form_for([@project, @project.tasks.build]) do |f| %> 
    <%= f.text_field :title, placeholder: 'Add a Task' %> 
    <%= f.submit %> 
<% end %> 

Aber wenn ich die Form über dem Block positionieren erhalte ich eine Fehlermeldung, meine Task-ID fehlt:

Meine beste Vermutung ist, dass die Build-Methode in meiner Form der Schuldige ist. Ich habe versucht, .build mit .new zu ersetzen, aber es hilft nicht. Hier

ist die Aktion erstellen von meinen Aufgaben Controller:

def create 
    @project = Project.find(params[:project_id]) 
    @task = @project.tasks.create(task_params) 
    redirect_to @project 
end 

Weiß jemand, warum ich nicht die Form über dem Block platzieren können?

+0

Da keine Aufgaben gibt. Warum nicht das Setup im Controller machen? –

Antwort

0

Überarbeitete Antwort. Ich habe mich über dieses Szenario lustig gemacht, um zu überprüfen, was passiert, und das Folgende ist ein funktionierendes Beispiel, von dem ich glaube, dass es das erreicht, was Sie wollen.

Bild des Endergebnisses: enter image description here

ich glaube, das Problem, das Sie hatten mehr von dem Controller-Setup ist. Sehen Sie sich die Kommentare in der Indexaktion und die Erstellungsaktion unten an, um zu sehen, wie ich die Aufgabe aufgebaut habe.

Routen:

# config/routes.rb 
Rails.application.routes.draw do 
    resources :projects do 
    resources :tasks 
    end 
    root 'tasks#index' 
end 

Controller-Aufgaben:

# app/controllers/tasks_controller.rb 
# un-modified actions left out. 
class TasksController < ApplicationController 
    before_action :set_task, only: [:show, :edit, :update, :destroy] 

    # GET /tasks 
    # GET /tasks.json 
    def index 
    # Create the parent project from the url id 
    @project = Project.find(params[:project_id]) 
    # Create a new task from the params from the form 
    @task = @project.tasks.new 
    # Get all tasks for your list 
    @tasks = @project.tasks.all 
    end 

    # POST /tasks 
    # POST /tasks.json 
    def create 
    # Create the parent project from the url id 
    @project = Project.find(params[:project_id]) 
    # Create a new task from the params from the form 
    @task = @project.tasks.new(task_params) 
    # try and save the task. 
    respond_to do |format| 
     if @task.save 
     format.html { redirect_to @project, notice: 'Task was successfully created.' } 
     else 
     format.html { render :index } 
     end 
    end 
    end 
end 

Aufgaben Indexansicht:

#app/views/tasks/index.html.erb 
p id="notice"><%= notice %></p> 

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

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

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

<h1>Listing Tasks</h1> 

<table> 
    <thead> 
    <tr> 
     <th>Name</th> 
     <th colspan="3"></th> 
    </tr> 
    </thead> 

    <tbody> 
    <% @tasks.each do |task| %> 
     <tr> 
     <td><%= task.name %></td> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

<br> 
+0

Danke für die ausführliche Antwort und Beispiel! Ich kann sehen, wie dies ein besseres System ist, Build/create/neue Methoden in der Steuerung zu halten. Ich habe viel daraus gelernt. – interfab

Verwandte Themen