在铁轨控制器中检索当前参数值 - 错误:可以找到没有ID的问题

发布于 2025-01-22 00:52:53 字数 1178 浏览 3 评论 0原文

单击删除后,我希望我的页面重定向到当前用户的民意调查。这是这样的。 Question_controller.rb:

  def destroy
    @poll_id = Question.find(param[:poll_id])
    @question.destroy

    respond_to do |format|
      format.html { redirect_to user_poll_url(current_user.id, @question.poll_id), notice: "Question was successfully destroyed." }
      format.json { head :no_content }
    end
  end

Question_controller的内容与以上更改外生成的导轨相同。

Question_Controller将poll_id作为一个参数。如何检索用于重定向的当前价值。

def question_params
  params.require(:question).permit(:poll_id, :title, :description)
end

routes.rb

  resources :users do
    resources :polls
  end
  resource :polls do
    resources :questions 
  end

我遇到的错误是:

ActiveRecord::RecordNotFound in QuestionsController#destroy
Couldn't find Question without an ID

Extracted source (around line #52):           
  # DELETE /questions/1 or /questions/1.json
  def destroy
    @poll_id = Question.find(param[:poll_id])
    session[:return_to] ||= request.referer
    @question.destroy

第52行为@poll_id = Question.find(param [:poll_id])>

Upon clicking delete I want my page to redirect to the current user's poll. This something like this.
questions_controller.rb:

  def destroy
    @poll_id = Question.find(param[:poll_id])
    @question.destroy

    respond_to do |format|
      format.html { redirect_to user_poll_url(current_user.id, @question.poll_id), notice: "Question was successfully destroyed." }
      format.json { head :no_content }
    end
  end

The Questions_controller's content is the same as the rails generated with the exception of the above change.

the question_controller has the poll_id as a paramter. How do I retrieve it's current value to use for redirection.

def question_params
  params.require(:question).permit(:poll_id, :title, :description)
end

routes.rb

  resources :users do
    resources :polls
  end
  resource :polls do
    resources :questions 
  end

The error i'm getting is:

ActiveRecord::RecordNotFound in QuestionsController#destroy
Couldn't find Question without an ID

Extracted source (around line #52):           
  # DELETE /questions/1 or /questions/1.json
  def destroy
    @poll_id = Question.find(param[:poll_id])
    session[:return_to] ||= request.referer
    @question.destroy

with line 52 being @poll_id = Question.find(param[:poll_id])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

他是夢罘是命 2025-01-29 00:52:53

param [:poll_id]不正确。您总是在名为params的方法中的控制器中获取这些属性poll_id您需要使用:

question_params[:poll_id]

等同于:

params[:question][:poll_id]

不确定您是否poll_id等于ID保存在问题中表。 查找方法将始终通过ID找到:这意味着:

Question.find(param[:poll_id])

如果

SELECT * FROM questions WHERE id = param[:poll_id]

poll_id等于ID,那么很好,或者您可能需要替换 find_by或还可以使用params [:ID]而不是poll_id poll_id 代码>如果存在(取决于您的路线)。

请参见更多信息: https:> https:> https:// https:https:// https:https:https:httper /a>

param[:poll_id] is not correct. You always get these attributes in the controller in the method named params and as we submit a form from the front end Rails maps them to a specific key (name of the model singular), for example, to get the poll_id you need to use:

question_params[:poll_id]

which is equivalent to:

params[:question][:poll_id]

Also not sure if you poll_id would equal to the id saved in questions table. The find method would always find by id which means that:

Question.find(param[:poll_id])

means

SELECT * FROM questions WHERE id = param[:poll_id]

So if poll_id is equivalent to id then that is fine or you may want to replace find by find_by or you can also use params[:id] instead of poll_id if that exists (depends on your routes).

More information here on params: https://edgeguides.rubyonrails.org/action_controller_overview.html#parameters

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