I have a controller named Projects. When I click on a button on the show page I execute a method which is in Projects. I want to find the ID of the current Project in this method.
Here is the controller
class ProjectsController < ApplicationController
#add idea from projects/show
def add_idea
@data = JSON.parse(params[:data])
data_splited = @data["get_idea"].split('|')
@idea = Idea.new
@idea.title = data_splited[0]
@idea.description = data_splited[1]
@idea.nb_of_votes = 0
project = Project.find(params[:id]) #this doesn't work
end
And this is the coffeescript code corresponding to the button :
adding_idea_to_project = ->
$("#add_idea").click -> get_idea_title()
get_idea_title = ->
title = document.getElementById("title").value
description = document.getElementById("description").value
bank_check = document.getElementById("bank")
if bank_check.checked == true
data = "{\"get_idea\": \"#{title}|#{description}|#{bank_check}\"}"
else
data = "{\"get_idea\": \"#{title}|#{description}\"}"
$.ajax({
type: "POST",
url: "projectss/idea.json",
data: "data=" + data
})
location.reload(true);
The routing is correct and is working, I just can’t find the ID of the Project in the given method.
Any Ideas
Wild guess: You need to provide the ID as part of your AJAX request: