【问题标题】:Edit in modal with rails and bootstrap使用 rails 和 bootstrap 进行模态编辑
【发布时间】:2017-03-16 10:42:32
【问题描述】:

我正在开发一个简单的待办事项列表。我想为每个列表项设置删除和编辑按钮。我还想在模态窗口中编辑和创建打开。现在这适用于创建,我不知道如何使它与编辑一起工作(现在显示模式窗口,但它是一个创建窗口)。

这是我的 index.html:

<div class="container-fluid">
    <div class="row">
        <h1 class="text-left">Task List</h1>
        <button type="button" class="btn btn-primary pull-right" data-toggle="modal" data-target="#myModal">New task</button>
    </div>
    <div class="row button-margin ">
    <% @tasks.each do |task| %>
            <div class="panel <%= task_status(task) %>">
                <div class="panel-heading">
                    <%= task.title %>
                    <%= link_to task_path(task),  class:"btn btn-link pull-right", method: :delete, data: { confirm: 'Are you sure?' } do %>
                        <span class="glyphicon glyphicon-remove" style="color:gray"></span>
                    <% end %>
                    <!-- <button type="submit" class="btn btn-link pull-right"> -->
                    <%= link_to edit_task_path(task), class:"btn btn-link pull-right", remote:true, "data-toggle" => "modal",  "data-dismiss=" => "modal", "data-target" => "#myModal" do %>
                        <span class="glyphicon glyphicon-pencil" style="color:gray"></span>
                <!--    </button> -->
                    <% end %>
                </div>
                <div class="panel-body">    
                    <h3><%= task.body %></h3>
                </div>
            </div>
        <% end %>
    </div>

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

这是带有模态的 _form partial

  <div id="myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">New task</h4>
        </div>      
        <div class="modal-body">
          <%= form_for @task, :html => {class:"form-horizontal"} do |f|%>
            <div class="form-group">
              <label for="inputTitle" class="col-sm-2 control-label">Title</label>
              <div class="col-sm-10">
                  <%= f.text_field :title, class:"form-control", id:"inputTitle", placeholder:"Title" %>
              </div>
            </div>
            <div class="form-group">
              <label for="inputBody" class="col-sm-2 control-label">Task</label>
              <div class="col-sm-10"> 
                <%= f.text_area :body, class:"form-control", id:"inputBody", placeholder:"Task text", rows:"3"%>
              </div>
            </div>
            <div class="form-group">
              <label for="dueDate" class="col-sm-2 control-label">Date</label>
              <div class="col-sm-10">
                <%= f.datetime_local_field :dueDate, class:"form-control", id:"dueDate"%>
              </div>
            </div>
            <div class="modal-footer">
              <%= f.submit class:"btn btn-primary"%>
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
          <% end %>
        </div>
      </div>
    </div>
  </div>

还有任务控制者:

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

    def new
        @task=Task.new
    end

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

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

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

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

        if @task.update(task_params)
            redirect_to tasks_path
        else
            render 'edit'
        end
    end

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

        redirect_to tasks_path
    end

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

谁能解释我做错了什么?为什么表单打开但未填写所选任务?

【问题讨论】:

  • 你有多少模态?你有一个create 和一个edit 或者你在同一个模式上运行两者?模态是指 bootstrap 模态,对吗?
  • 是的,它的引导模式,现在我有一个用于编辑和创建。

标签: ruby-on-rails twitter-bootstrap ruby-on-rails-4


【解决方案1】:

在edit.html.erb中

<%= render "tasks/form" %>

在 index.html.erb 中为编辑页面添加 ajax 调用,获取编辑页面并以模态显示。

【讨论】:

    【解决方案2】:

    您在link_to edit_task_path(task) 中使用remote: true。这样,任务将异步加载,页面不会刷新。这就是为什么您的表格没有被填写的原因。

    另外,使用相同的链接来加载任务并打开模式。我认为您需要更好的方法。

    【讨论】:

    • 删除 remote:true 没有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多