Angela Inniss
Angela Inniss

Reputation: 359

My CREATE & DELETE routes not working do not understand why?

Hello I am trying to create a bookmark feature in my app. I have created the model I want to bookmark (Hairstyle), I also have a User model and a "Saved_Hairstyle" model which is the join table in the scenario. In my routes.rb file I added a CREATE & DELETE route. In my controller I wrote out the CREATE & DELETE methods. I then proceeded to link_to my CREATE & DELETE methods paths in my View.

I would like that when I click on the CREATE Link a POST method is fired so that I can show an element on the page as being "bookmarked" and when I click the DELETE link a DELETE method is fired so that I can show an element on the page as being "unboomarked" but they don't work. When I do RAILS ROUTES in Rails C I can see the right pathways but when I click through the links don't do anything. Repo for ease of understanding: https://github.com/Angela-Inniss/hair-do

routes.rb

Rails.application.routes.draw do
  root to: 'pages#home'
  devise_for :users
  resources :hairstyles do
    member do
      put "like", to: "hairstyles#upvote"
      put "dislike", to: "hairstyles#downvote"
    end
    resources :comments, only: :create
    resources :saved_hairstyles, only: [:new,:create]
  end
  resources :saved_hairstyles, only: :destroy
  resources :comments, only: :destroy
  resources :hairdressers
end

class SavedHairstylesController < ApplicationController
  def create    
    @hairstyle = Hairstyle.find(params[:hairstyle_id])  
    @saved_hairstyle = SavedHairstyle.new(user: current_user, hairstyle: @hairstyle)   
    if @saved_hairstyle.save
      respond_to do |format|       
        format.html { redirect_to hairstyle_path(@saved_hairstyle.hairstyle) }
        format.js  # <-- will render `app/views/comments/create.js.erb`
      end
    else
      respond_to do |format|
        format.html { render 'hairstyles' }
        format.js  # <-- idem
      end
    end
  end
end

  def destroy    
    @saved_hairstyle = SavedHairstyle.find(params[:id])
    @saved_hairstyle.destroy
    @hairstyle = @saved_hairstyle.hairstyle
    respond_to do |format|
      format.html { redirect_to hairstyle_path(@saved_hairstyle.hairstyle }
      format.js
    end
  end

view.html.erb

   <div class="bookmark">

              <% saved_hairstyle = SavedHairstyle.find_by(user: current_user, hairstyle: hairstyle.id) %>

              <% if saved_hairstyle %>
                <%= link_to  hairstyle_saved_hairstyle_path(saved_hairstyle), method: :post do %>
                    <i class="fas fa-plus"></i>
                 <% end %>

              <% else %>
                <%= link_to saved_hairstyle_path(hairstyle), method: :delete do %>
                    <i class="fas fa-plus-circle"></i>
                <% end %>
              <% end %>
            </div>

create.js.erb file (this is what I Would like to happen on the POST request (i have something similar for the DELETE request)


plusCircle = document.getElementById("bookmark");
plusCircle.innerHTML = `<%= link_to  '#', method: :post do %>
                    <i class="fas fa-plus"></i>
                 <% end %>`

Upvotes: 0

Views: 68

Answers (1)

EJAg
EJAg

Reputation: 3298

There seems to be some errors in your view logic. You are loading saved hairstyles and try to book mark it again. You're also trying to delete a saved hairstyle if it doesn't exist. The path helpers also seem to be wrong (delete method is not nested and nested resources should be plural). Maybe it should look something like this:

   <div class="bookmark">
      <% saved_hairstyle = SavedHairstyle.find_by(user: current_user, hairstyle: hairstyle.id) %>
      <% if saved_hairstyle %>
        <%= link_to saved_hairstyle_path(saved_hairstyle), method: :delete do %>
           <i class="fas fa-plus-circle"></i>
         <% end %>
       <% else %>
          <%= link_to  hairstyle_saved_hairstyles_path(hairstyle), method: :post do %>
            <i class="fas fa-plus"></i>
          <% end %>
        <% end %>
    </div>

By the way, if you check your console, you probably will see some errors that would point you to the right direction. You should also move saved_hairstyle query to a helper method.

Upvotes: 2

Related Questions