0

In my app I have models called weeklies and scores. Each weekly has_many scores and each score belongs_to a weekly and a user. I have the create and show methods working just fine, but when I try to delete a score I get Couldn't find Weekly without an ID.

My weekly#show page is where all the magic happens:

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

Since this is the weekly#show page, here's my weeklies_controller, which in my head should have jurisdiction:

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

However, since we're deleting a score and the error cites the scores controller (see marked line), here's the 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

Here are the appropriate routes:

         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

Can anyone help figure out where this error is coming from?

uday
  • 8,544
  • 4
  • 30
  • 54
Liz
  • 1,369
  • 2
  • 26
  • 61
  • what happens if you flash[:notice] = @weekly? – Boltz0r Jun 02 '16 at 14:59
  • in the destroy method – Boltz0r Jun 02 '16 at 14:59
  • The error is on the first line of the destroy method (sorry, just edited the post to indicate this) so I don't think it's even getting to the flash notices yet. – Liz Jun 02 '16 at 15:01
  • no my friend what i mean is what happens if you comment all the code in destroy method and just do this : @weekly = Weekly.friendly.find(params[:weekly_id]) flash[:notice] = weekly It should give u the value of weekly which I'm quite sure is the problem – Boltz0r Jun 02 '16 at 15:03
  • The flash notice didn't work (I just errored at the same point), but I'm using the `better_errors` gem, so I typed in @weekly in the error message console and it said it was nil. – Liz Jun 02 '16 at 15:06
  • ok then show me the POST params of the delete method – Boltz0r Jun 02 '16 at 15:06
  • anyway just try to change params[:weekly_id] to params["weekly_id"] – Boltz0r Jun 02 '16 at 15:07
  • Still get the same error with `@weekly = Weekly.friendly.find(params[":weekly_id"])` – Liz Jun 02 '16 at 15:23
  • i really need to see that POST params – Boltz0r Jun 02 '16 at 15:23
  • If you wana delete a weekly then why are you not doing just `@weekly = Weekly.find(params[":weekly_id"])` what does the `Weeky.friendly` mean here? – uday Jun 02 '16 at 15:28
  • Friendly_Id is a gem that displays slugs instead of id numbers in your url. – Liz Jun 02 '16 at 15:29
  • And I added the routes to the original post. I think that's what you mean by POST params, but if it isn't let me know what you need and I'll post it. – Liz Jun 02 '16 at 15:30
  • And I'm trying to delete the score, not the weekly itself. – Liz Jun 02 '16 at 15:32

1 Answers1

0

In the end this issue was fixed by nesting my routes like this:

resources :weeklies do
  resources :scores
end

I had to change the HTML from:

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

to

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

And this eliminated the error.

Liz
  • 1,369
  • 2
  • 26
  • 61