如何在rails中的show方法中的其他地方找到对象的id

发布于 2024-11-28 21:53:42 字数 1105 浏览 0 评论 0原文

我有一个名为“项目”的控制器。当我单击显示页面上的按钮时,我执行项目中的一个方法。我想在这个方法中找到当前项目的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红玫瑰 2024-12-05 21:53:42

大胆猜测:您需要提供 ID 作为 AJAX 请求的一部分:

 url: "projects/add_idea/#{projectId}.json"

Wild guess: You need to provide the ID as part of your AJAX request:

 url: "projects/add_idea/#{projectId}.json"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文