简体   繁体   中英

Rails add new and edit form in bootstrap modal

I'm new with 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:

  <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:

<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 .

Whats wrong with my code? And how can I pass an existing task to the form for Edit action?

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 [] .

You could "hack" the Rails behavior and explictly declare your form like this:

<%= 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.

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:

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.

Error message specify that first argument ie @task is nil.

So change your controller code

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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