如何使用"的link_to"而不是"复选框"对于POST?如何使用、复选框、而不是、link_to

2023-09-10 17:47:01 作者:泪湿了眼角

我们的想法是,当用户有一个 missed_day (又名罢工),他将单击此按钮。他目前可以点击复选框来标记 missed_day

The idea is that when the user has a missed_day (aka a strike) he would click this button. He can currently click a checkbox to mark a missed_day.

习惯/节目

<div class="strikes">
  <% if @habit.current_level_strike %> 
    <div class="btn" id="red"> <label id="<%= @habit.id %>" class="habit-id">Strikes:</label>
  <% else %> 
    <div class="btn" id="gold"> <label id="<%= @habit.id %>" class="habit-id-two">Strikes:</label>
  <% end %>
    <% @habit.levels.each_with_index do |level, index| %>
      <% if @habit.current_level >= (index + 1) %>
        <p data-submit-url="<%= habit_level_days_missed_index_path({ habit_id: @habit.id, level_id: level.id }) %>"
           data-delete-url="<%= habit_level_days_missed_path({ habit_id: @habit.id, level_id: level.id, id: 1 }) %>">
          <% if @habit.current_level_strike %> 
            <label id="<%= level.id %>" class="level-id">Level <%= index + 1 %>:</label> 
          <% else %> 
            <label id="<%= level.id %>" class="level-id-two">Level <%= index + 1 %>:</label> 
          <% end %>
          <%= check_box_tag nil, true, level.missed_days > 0, {class: "habit-check"} %>
          <%= check_box_tag nil, true, level.missed_days > 1, {class: "habit-check"} %>
          <%= check_box_tag nil, true, level.missed_days > 2, {class: "habit-check"} %>
       </p>
      <% end %>
    <% end %>
  </div>
</div>

habit.js

$(document).ready(function()
{
  $(".habit-check").change(function()
  {
    var submitUrl = $(this).parents("p").data("submit-url");
    var deleteUrl = $(this).parents("p").data("delete-url");

    if($(this).is(":checked"))
    {
       $.ajax(
       {
         url: submitUrl,
         method: "POST"
       })
       .done(function () {
         location.reload();
       });
    }
    else
    {
       $.ajax(
       {
         url: deleteUrl,
         method: "DELETE"
       })
       .done(function () {
         location.reload();
       });
    }
  });
});

days_missed_controller

class DaysMissedController < ApplicationController
  before_action :logged_in_user, only: [:create, :destroy]
  before_action :correct_user, only: [:create, :destroy]

  def create
    habit = Habit.find(params[:habit_id])
    habit.missed_days = habit.missed_days + 1
    @habit.save!
    level = habit.levels.find(params[:level_id])
    level.missed_days = level.missed_days + 1
    if level.missed_days == 3
      level.missed_days = 0
      level.days_lost += habit.calculate_days_lost + 2
    end
    level.save!
    head :ok # this returns an empty response with a 200 success status code
  end

  def destroy
    habit = Habit.find(params[:habit_id])
    habit.missed_days = habit.missed_days - 1
    @habit.save!
    level = habit.levels.find(params[:level_id])
    level.missed_days = level.missed_days - 1
    level.save!
    head :ok # this returns an empty response with a 200 success status code
  end

private

  def correct_user
    @habit = current_user.habits.find(params[:habit_id])
    redirect_to habits_path, notice: "Not authorized to edit this habit" if @habit.nil?
  end
end

第1 PIC是从主页。这里是code两个按钮:

The 1st pic is from the home page. Here is the code for the two buttons:

<%= link_to '<span class="glyphicon glyphicon-ok"></span>'.html_safe, mark_completed_path(habit), remote: true, method: 'put', class: 'update_habit', id: 'home_check' %>
<%= link_to '<span class="glyphicon glyphicon-remove"></span>'.html_safe, habit_level_days_missed_index_path({ habit_id: @habit.id, level_id: level.id }), remote: true, method: 'put', class: 'habit-check' %>

我们如何能得到这个第二个按钮的工作?这种试图给了我未定义的方法'身份证'的零:NilClass

How can we get this 2nd button to work? This attempt gave me undefined method 'id' for nil:NilClass

下面是在 主旨是

Here's the Gist of it.

推荐答案

您需要更改此链接

<%= link_to '<span class="glyphicon glyphicon-remove"></span>'.html_safe, habit_level_days_missed_index_path({ habit_id: @habit.id, level_id: level.id }), remote: true, method: 'put', class: 'habit-check' %>

<%= link_to '<span class="glyphicon glyphicon-remove"></span>'.html_safe, habit_level_days_missed_index_path({ habit_id: habit, level_id: habit.current_level }), remote: true, method: 'put', class: 'habit-check' %>