找不到没有 ID 的每周(在 Rails 中删除)

Couldn't find Weekly without an ID (Deleting in Rails)

在我的应用程序中,我有名为 weekliesscores 的模型。每个每周 has_many 得分,每个得分 belongs_to 一个每周和一个用户。我的创建和显示方法工作得很好,但是当我尝试删除乐谱时,我得到 Couldn't find Weekly without an ID.

我的 weekly#show 页面是所有魔法发生的地方:

<h2>Your Previous Results</h2>
   <% @weekly.scores.each do |score| %>
     <p>
       <%= score.number %> Repetitions, <%= time_ago_in_words(score.created_at) %> ago 
       (<% if score.user_id = current_user %>
          <%= link_to 'Delete', [score], method: :delete %>
       <% end %> )
     </p>
   <% end %>

因为这是weekly#show页面,这里是我的weeklies_controller,在我看来应该有管辖权:

class WeekliesController < ApplicationController
  def index
    @weeklies = Weekly.all.order("created_at DESC")
    @most_recent_weekly = Weekly.order("created_at").last
  end

  def show
    @weekly = Weekly.friendly.find(params[:id])
    @score = Score.new
  end

  def new
    @weekly = Weekly.new
    @weekly.user_id = current_user
  end

  def create
    @weekly = Weekly.new(weekly_params)
    @weekly.user = current_user

    if @weekly.save
      flash[:notice] = "Workout of the week was saved successfully."
      redirect_to @weekly
    else
      flash.now[:alert] = "Error creating workout. Please try again."
      render :new
    end
  end

  def edit
    @weekly = Weekly.friendly.find(params[:id])
    @weekly.user_id = current_user
  end

  def update
    @weekly = Weekly.friendly.find(params[:id])

    @weekly.name = params[:weekly][:name]
    @weekly.teaser = params[:weekly][:teaser]
    @weekly.instructions = params[:weekly][:instructions]
    @weekly.difficulty = params[:weekly][:difficulty]
    @weekly.video = params[:weekly][:video]
    @weekly.user = current_user

    if @weekly.save
       flash[:notice] = "Workout was updated successfully."
      redirect_to @weekly
    else
      flash.now[:alert] = "Error saving workout. Please try again."
      render :edit
    end
  end

  def destroy
    @weekly = Weekly.friendly.find(params[:id])

    if @weekly.destroy
      flash[:notice] = "\"#{@weekly.name}\" was deleted successfully."
      redirect_to action: :index
    else
      flash.now[:alert] = "There was an error deleting the workout."
      render :show
    end
  end

  private
  def weekly_params
    params.require(:weekly).permit(:name, :teaser, :instructions, :video, :difficulty, :slug, :user_id)
  end
end

但是,由于我们正在删除分数并且错误引用了分数控制器(见标记行),这里是 scores_controller:

class ScoresController < ApplicationController
  before_action :authenticate_user!


  def index
      @weekly = Weekly.friendly.find(params[:weekly_id])
      @score = Score.new
      @scores = Score.all
  end

  def new
    @score = Score.new
  end

  def create
    @weekly = Weekly.friendly.find(params[:weekly_id])
    score = @weekly.scores.new(score_params)
    score.user = current_user

    if score.save
      flash[:notice] = "Results saved successfully."
      redirect_to [@weekly]
    else
      flash[:alert] = "Results failed to save."
      redirect_to [@weekly]
    end
  end

  def destroy
    @weekly = Weekly.friendly.find(params[:weekly_id])  <!-- ERROR CALLED ON THIS LINE -->
    score = @weekly.scores.find(params[:id])

    if score.destroy
      flash[:notice] = "Results were deleted successfully."
      redirect_to [@weekly]
    else
      flash[:alert] = "Results couldn't be deleted. Try again."
      redirect_to [@weekly]
    end
  end

  private

  def score_params
      params.require(:score).permit(:number, :user_id, :weekly_id)
  end
end

以下是合适的路线:

         reports POST   /reports(.:format)                     reports#create
          report DELETE /reports/:id(.:format)                 reports#destroy
        weeklies GET    /weeklies(.:format)                    weeklies#index
                 POST   /weeklies(.:format)                    weeklies#create
      new_weekly GET    /weeklies/new(.:format)                weeklies#new
     edit_weekly GET    /weeklies/:id/edit(.:format)           weeklies#edit
          weekly GET    /weeklies/:id(.:format)                weeklies#show
                 PATCH  /weeklies/:id(.:format)                weeklies#update
                 PUT    /weeklies/:id(.:format)                weeklies#update
                 DELETE /weeklies/:id(.:format)                weeklies#destroy
          scores GET    /scores(.:format)                      scores#index
                 POST   /scores(.:format)                      scores#create
       new_score GET    /scores/new(.:format)                  scores#new
      edit_score GET    /scores/:id/edit(.:format)             scores#edit
           score GET    /scores/:id(.:format)                  scores#show
                 PATCH  /scores/:id(.:format)                  scores#update
                 PUT    /scores/:id(.:format)                  scores#update
                 DELETE /scores/:id(.:format)                  scores#destroy

任何人都可以帮助找出此错误的来源吗?

最后这个问题通过像这样嵌套我的路线得到解决:

resources :weeklies do
  resources :scores
end

我不得不将 HTML 更改为:

<%= link_to 'Delete', [score], method: :delete %>

<%= link_to 'Delete', [@weekly, score], method: :delete %>

这消除了错误。