简体   繁体   English

在rails上的create controller ruby​​中传递新对象ID

[英]Pass new object id within create controller ruby on rails

I'm hoping this is easy.... =) 我希望这很容易.... =)

I've got a question object, and a favanswer object, question has_one favanswer, favanswer belongs_to question. 我有一个问题对象,一个favanswer对象,一个问题has_one favanswer,一个favanswer includes_to问题。 I've got a favanswer_id column and index in my question model, I'm trying to pass the newly posted favanswer_id to the corresponding question objects' favanswer_id field. 我的问题模型中有一个favanswer_id列和索引,我正在尝试将新发布的favanswer_id传递到相应问题对象的favanswer_id字段。 Maybe it's a lot more..or less..complicated than this,.but in the favanswer_controller I have the below in the create action, and I'm just trying to figure out how to correct line 6, or whatever I need to do, because without 6 it works fine, it just doesn't update my question model...Help? 也许比这复杂得多..或更少..但是在favanswer_controller中,我在create动作中包含以下内容,而我只是想弄清楚如何纠正第6行,或者我需要做的事情,因为没有6,它可以正常工作,只是不更新​​我的问题模型...帮助吗?

  1 def create
  2
  3  @answer = Answer.find(params[:answer_id])
  4  @question = @answer.question_id
  5  Favanswer.create(favanswer_params.merge(user: current_user, question_id: @question))
  6  Question.update(@question, :favanswer_id => "HOW DO I PASS THE NEW FAVANSWER ID HERE?")
  7  redirect_to question_path(@question)
  8
  9 end

I think the mistake you're making here is referring to the foreign key of the association directly. 我认为您在这里犯的错误是直接引用关联的外键。 Instead, rely on the association as an object. 而是将关联作为对象。 When you create the Favanswer , pass in the @answer.question object and not the id. 创建Favanswer ,请传递@answer.question对象,而不要传递ID。 Additionally, don't pass it in to question_id but rather to question 另外,不要将其传递给question_id ,而是要question

def create
  @answer = Answer.find(params[:answer_id])
  @question = @answer.question
  Favanswer.create(favanswer_params.merge(user: current_user, question:     @question))
  redirect_to question_path(@question)
end

Rails is friendly and kind and when you assign the Question instance to the named association, it will hook it up as required, so you have no need to update the Question directly. Rails友好友好,当您将Question实例分配给命名的关联时,它将根据需要进行连接,因此您无需直接更新Question。

Model.create returns an instance of the newly created Model, which is useful for what you're trying to do. Model.create返回新创建的Model的实例,这对于您尝试执行的操作很有用。 That means: 这意味着:

@favanswer = Favanswer.create(favanswer_params.merge(user: current_user, question_id: @question))
Question.update(@question, :favanswer_id => @favanswer.id)

Will accomplish what you're after. 会成就您的追求。

smh... 嗯...

figured it out =) 想通了=)

@L = Favanswer.create(favanswer_params.merge(user: current_user, question_id:    @question))
Question.update(@question, :favanswer_id => @L.id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将ID传递给控制器​​中的动作-Ruby on Rails - How to pass id to an action in the controller - Ruby on Rails Ruby on Rails-通过另一个控制器传递ID - Ruby on Rails - Pass ID from another controller Ruby on rails 控制器创建新参数 - Ruby on rails controller create new params 如何从控制器Ruby On Rails中传递表中的用户ID - How to pass user id in table from controller Ruby On Rails Ruby on Rails:需要将下拉的ID /值传递回控制器 - Ruby on rails: Need to pass drop down ID/value back to the controller 无法为Ruby on Rails模型创建新对象 - Cannot create new object for Ruby on Rails model Ruby on Rails,将图像从一个 object 传递到另一个或从 NEW 传递到 CREATE - Ruby on Rails, pass Image from one object to another or from NEW to CREATE 如何将JavaScript值作为json对象传递到Rails中的ruby中的控制器? - How to pass the javascript value as json object to the controller in ruby on rails? 如何在 Ruby on Rails 中为控制器中的不同环境使用不同的对象? - How to use a different object for different environments in the controller within Ruby on Rails? 在控制器中创建一个全新的动作会更好还是在Ruby on Rails中创建一个新的控制器? - Would it be better to create an entirely new action in my controller or should I create a new controller in Ruby on Rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM