Reputation: 89
I'm getting an error on my set_project method in my projects controller. According to the error screen I am seeing, the parameters being passed are as follows:
{"project_id"=>"8", "id"=>"add_vote"}
I think the number 8 is what should be passed. Here is my projects controller:
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]
before_action :set_all_votes, only: [:show, :update]
# GET /projects
# GET /projects.json
def index
@projects = Project.all
@votes = Vote
end
# GET /projects/1
# GET /projects/1.json
def show
end
# GET /projects/new
def new
@project = Project.new
end
# GET /projects/1/edit
def edit
end
# POST /projects
# POST /projects.json
def create
@project = Project.new(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
respond_to do |format|
if @project.update(project_params)
format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { render :show, status: :ok, location: @project }
else
format.html { render :edit }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# DELETE /projects/1
# DELETE /projects/1.json
def destroy
@project.destroy
respond_to do |format|
format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
format.json { head :no_content }
end
end
def add_vote
unless params[:project_id].nil?
@project = Project.find(params[:project_id])
@newVote = Vote.create!(vote_value: true, user_id: current_user.id, project_id: @project.id)
respond_to do |format|
format.html { redirect_to @project, notice: 'Your vote has been recorded'}
format.json { render :show, status: :ok, location: @project }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.find(params[:id])
end
def set_all_votes
@votes = Vote.all
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:title, :video_url, :description, :images, :team_name, :vote_count)
end
end
Here is my projects show page. Clicking the "Vote for this project" button is what triggers the error.
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= @project.title %>
</p>
<p>
<strong>Video url:</strong>
<%= @project.video_url %>
</p>
<iframe src='https://www.youtube.com/watch?v=HbW-Bnm6Ipg' frameborder='0' allowfullscreen></iframe>
<p>
<strong>Description:</strong>
<%= @project.description %>
</p>
<br><br>
<div class="voteButton">
<%= link_to "<button>Vote for this project</button>".html_safe, add_vote_path(:project_id => @project.id) %>
</div>
<br><br>
<%= link_to 'Edit', edit_project_path(@project) %> |
<%= link_to 'Back', projects_path %>
Here is my routes file:
Rails.application.routes.draw do
devise_for :users
resources :votes
resources :projects
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'projects#index'
controller :projects do
get '/projects/add_vote' => 'projects#add_vote', as: :add_vote
end
end
Upvotes: 1
Views: 46
Reputation: 787
If you type rake routes
in console you will see that due to your resources :projects
you have a /projects/:id
route which is what /projects/add_vote
is actually being pattern-matched to. When a new request comes in the router will look for a route match going from top to bottom of the rake routes list. To fix this you could move the lien with resources :projects
below add_vote
.
Also, I think it would be helpful to change your add_vote path to include the id of the project:
get '/projects/:id/add_vote' => 'projects#add_vote', as: :add_vote
This will have the added benefit of not matching /projects/:id
as your current situation does. Your link then becomes:
<%= link_to "<button>Vote for this project</button>".html_safe, add_vote_path(@project) %>
Upvotes: 1
Reputation: 5491
Your route doesn't currently specify that it needs a parameter. Try this for your route instead:
controller :projects do
get 'add_vote/:project_id', to: 'projects#add_vote', as: :add_vote
end
rails routes
is a useful tool for debugging routing issues.
Upvotes: 2