带Rails的AJAX-创建的项目未显示

时间:2018-09-24 21:01:16

标签: ruby-on-rails ajax

我在以前的应用程序上能很好地工作,但是当我将代码复制到新的应用程序(并重新设置CSS样式)时,它不起作用。

期望的行为是使任务在表单完成时异步地显示在正确的框中,在选中时将自己标记为已完成,然后删除。

当前行为是确实创建了项目,但它们从未填充到正确的框中。没有它们的出现,我无法确定其他行为是否按预期运行。

这是我的tasks#show视图中的HTML / ERB:

<% if current_user %>
      <div class="container">
            <div class="row">
                <div class="col-xs-12 col-md-6 col-lg-12">
                <div class="content-box">
                  <h2 class="font-script color-neutral text-center">One-Time Tasks</h2>
                        <div id="onetime-todo"><%= render partial: 'items', locals: { task: @one_time } %></div>
                        <div id="onetime-done"><%= render partial: 'done', locals: { task: @one_time_done } %></div>
                </div> <!-- content box -->
                </div> <!-- col -->

                <div class="col-xs-12 col-md-6 col-lg-4">
                <div class="content-box">
                  <h2 class="font-script color-neutral text-center">Daily</h2>
                        <p class="text-center">These automatically uncheck at night so you have a fresh list&nbsp;in&nbsp;the&nbsp;morning.</p>
                  <div id="daily-todo"><%= render partial: 'items', locals: { task: @daily } %></div>
                        <div id="daily-done"><%= render partial: 'done', locals: { task: @daily_done } %></div>
                </div> <!-- content box -->
                </div> <!-- col -->

                <div class="col-xs-12 col-md-6 col-lg-4">
                <div class="content-box">
                  <h2 class="font-script color-neutral text-center">Weekly</h2>
                        <p class="text-center">These automatically uncheck Sunday night so you get a new list&nbsp;each&nbsp;Monday&nbsp;morning.</p>
                  <div id="weekly-todo"><%= render partial: 'items', locals: { task: @weekly } %></div>
                        <div id="weekly-done"><%= render partial: 'done', locals: { task: @weekly_done } %></div>
                </div> <!-- content box -->
                </div> <!-- col -->

                <div class="col-xs-12  col-md-6 col-lg-4">
                <div class="content-box">
                  <h2 class="font-script color-neutral text-center">Monthly</h2>
                        <p class="text-center">These automatically uncheck on the last day of the month so you start with a clean&nbsp;list&nbsp;each&nbsp;1st.</p>
                  <div id="monthly-todo"><%= render partial: 'items', locals: { task: @monthly } %></div>
                        <div id="monthly-done"><%= render partial: 'done', locals: { task: @monthly_done } %></div>
                </div> <!-- content box -->
                </div> <!-- col -->
            </div> <!-- row -->
      </div> <!-- container -->
<% end %> <!-- current_user -->

我有create.js.erb文件:

$("#onetime-todo").html("<%= escape_javascript(render partial: 'items', locals: { task: @one_time }) %>")
$("#onetime-done").html("<%= escape_javascript(render partial: 'done', locals: { task: @one_time_done }) %>")

$("#daily-todo").html("<%= escape_javascript(render partial: 'items', locals: { task: @daily }) %>")
$("#daily-done").html("<%= escape_javascript(render partial: 'done', locals: { task: @daily_done }) %>")

$("#weekly-todo").html("<%= escape_javascript(render partial: 'items', locals: { task: @weekly }) %>")
$("#weekly-done").html("<%= escape_javascript(render partial: 'done', locals: { task: @weekly_done }) %>")

$("#monthly-todo").html("<%= escape_javascript(render partial: 'items', locals: { task: @monthly }) %>")
$("#monthly-done").html("<%= escape_javascript(render partial: 'done', locals: { task: @monthly_done }) %>")

$('#textField').val("");

还有tasks_controller.rb

class TasksController < ApplicationController
  before_action :set_task, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!
  before_action :set_variables, only: [:index, :create, :destroy, :check_task, :uncheck_task]


  def set_variables
    @user_tasks = Task.where(user_id: current_user.id)
    @one_time = Task.where(frequency: "OneTime", completed: false, user_id: current_user.id)
    @one_time_done = Task.where(frequency: "OneTime", completed: true, user_id: current_user.id)
    @daily = Task.where(frequency: "Daily", completed: false, user_id: current_user.id)
    @daily_done = Task.where(frequency: "Daily", completed: true, user_id: current_user.id)
    @weekly = Task.where(frequency: "Weekly", completed: false, user_id: current_user.id)
    @weekly_done = Task.where(frequency: "Weekly", completed: true, user_id: current_user.id)
    @monthly = Task.where(frequency: "Monthly", completed: false, user_id: current_user.id)
    @monthly_done = Task.where(frequency: "Monthly", completed: true, user_id: current_user.id)
  end

  # GET /tasks
  def index
    @create_task = Task.new
    @tasks = Task.all
  end

  # GET /tasks/new
  def new
    @task = Task.new
  end

  # GET /tasks/1/edit
  def edit
  end

  # POST /tasks
  def create
    @task = Task.new(task_params)
    @task.user_id = current_user.id
    if @task.save
      respond_to do |format|
        format.js
        format.html
      end
    else
      render :new
    end
  end

  # DELETE /tasks/1
  def destroy
    if @task.destroy
        respond_to do |format|
          format.js
          format.html
      end
    else
      flash[:warning] = "Oops! Something went wrong!"
    end
  end

  def check_task
      @task = Task.find(params[:id])
      if @task.update_attributes(completed: true)
          respond_to do |format|
            format.js
            format.html
          end
      else
          redirect_to tasks_path
          flash[:warning] = "Oops! Something went wrong!"
      end
  end

  def uncheck_task
      @task = Task.find(params[:id])
      if @task.update_attributes(completed: false)
          respond_to do |format|
            format.js
            format.html
          end
      else
          redirect_to tasks_path
          flash[:warning] = "Oops! Something went wrong!"
      end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_task
      @task = Task.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def task_params
      params.require(:task).permit(:name, :frequency, :completed, :user_id)
    end
end

以及相应的routes

  resources :tasks
  post "tasks/:id/check_task" => "tasks#check_task", as: "check_task"
  post "tasks/:id/uncheck_task" => "tasks#uncheck_task", as: "uncheck_task"

这两个应用程序之间的唯一变化是,新应用程序是Rails 5.2,而旧应用程序是4.2,那么在那里可能会有一些变化吗?除此之外,我不知道要看哪里。

有什么想法吗?

0 个答案:

没有答案