如何在rails中的show方法中的其他地方找到对象的id
我有一个名为“项目”的控制器。当我单击显示页面上的按钮时,我执行项目中的一个方法。我想在这个方法中找到当前项目的ID。 这是控制器
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
,这是与按钮对应的咖啡脚本代码:
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);
路由正确并且正在工作,我只是在给定的方法中找不到项目的 ID。 任何想法
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大胆猜测:您需要提供 ID 作为 AJAX 请求的一部分:
Wild guess: You need to provide the ID as part of your AJAX request: