简体   繁体   English

Rails在引导模式下添加新的和编辑的表单

[英]Rails add new and edit form in bootstrap modal

I'm new with Ruby on Rails. 我是Ruby on Rails的新手。 Now I'm trying to make a simple ToDo List app with bootstrap. 现在,我正在尝试使用引导程序制作一个简单的待办事项列表应用程序。

Everything works but I tried to render my New task form to a modal window and it's crashed. 一切正常,但我尝试将“新任务”表单呈现到模式窗口,并且崩溃了。 Here is my _form partial code: 这是我的_form部分代码:

  <div id="myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>      
      </div>
        <div class="modal-body">
          <h1>New Task</h1>
          <%= form_for @task, :html => {class:"form-horizontal"} do |f|%>
             <!-- Here is form code -->
          <% end %>
          <%= link_to 'Back', tasks_path %>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>

And here is my Index.html.erb: 这是我的Index.html.erb:

<div class="container-fluid">
    <div class="row">
        <h1 class="text-left">Task List</h1>
        <%= button_to 'New Task', new_task_path, :class =>"btn btn-success pull-right", :method => "get", data: {'data-toggle' => "modal", 'data-targe' => "#myModal"} %>
    </div>
    <div class="row button-margin ">
    <% @tasks.each do |task| %>
            <div class="panel <%= task_status(task) %>">
                <div class="panel-heading">
                    <%= task.title %>
                    <p class="pull-right"><%= task.dueDate %></p>
                </div>
                <div class="panel-body">    
                    <h3><%= task.body %></h3>
                </div>
            </div>
        <% end %>
    </div>

    <%= render "tasks/form" %>
</div>

So when trying to start my index page I get an error First argument in form cannot contain nil or be empty . 因此,当尝试启动我的索引页时,我收到一个错误First argument in form cannot contain nil or be empty

Whats wrong with my code? 我的代码有什么问题? And how can I pass an existing task to the form for Edit action? 以及如何将现有任务传递给表单以进行“编辑”操作?

UPD: UPD:

Method for new task in task controller is: 在任务控制器中用于新任务的方法是:

class TasksController < ApplicationController
    def index
        @tasks=Task.all
    end

    def new
        @task=Task.new
    end

    def show
        @task=Task.find(params[:id])
    end

    def create
        @task=Task.new(task_params)
        if @task.save
            redirect_to @task
        else
            render 'new'
        end
    end

    private
        def task_params
            params.require(:task).permit(:title, :body, :creationDate, :dueDate)
        end
end

The @task variable in your form_for can't be nil nor [] . form_for@task变量不能为nil[]

You could "hack" the Rails behavior and explictly declare your form like this: 您可以“破解” Rails行为并像下面这样明确声明您的表单:

<%= form_for Task.new, :html => {class:"form-horizontal"} do |f| %>
...

But it wouldn't be the "correct" way to use form_for , nor to use Rails, because you wouldn't be following the MVC patterns, so, instance variables from controllers to the views, no directly in the view. 但这既不是使用form_for也不是使用Rails的“正确”方法,因为您不会遵循MVC模式,因此,从控制器到视图的实例变量(而不是直接在视图中)。

As I can't see what's in your method in the tasks_controller , maybe you've forgot to set the Task.new value to your @task variable, try setting it as: 由于无法在tasks_controller看到您的方法中的tasks_controller ,也许您忘记了将Task.new值设置为@task变量,请尝试将其设置为:

class Task < ApplicationController

  def index
    @task = Task.new
    ...
  end
end

Hope it helps. 希望能帮助到你。

EDIT: You're trying to access to your new task form within your index file, so the @task = Task.new must be on your index method. 编辑:您正在尝试访问索引文件中的新任务表单,因此@task = Task.new必须在索引方法上。

Error message specify that first argument ie @task is nil. 错误消息指定第一个参数,即@task为nil。

So change your controller code 因此,更改您的控制器代码

def index
  @task = Task.new
  @tasks = Task.all
end

def edit
  @task = Task.find(params[:id])
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM