在铁轨控制器中检索当前参数值 - 错误:可以找到没有ID的问题
单击删除后,我希望我的页面重定向到当前用户的民意调查。这是这样的。 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
param [:poll_id]
不正确。您总是在名为params
的方法中的控制器中获取这些属性poll_id
您需要使用:等同于:
不确定您是否
poll_id
等于ID
保存在问题中
表。查找
方法将始终通过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 namedparams
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 thepoll_id
you need to use:which is equivalent to:
Also not sure if you
poll_id
would equal to theid
saved inquestions
table. Thefind
method would always find byid
which means that:means
So if
poll_id
is equivalent toid
then that is fine or you may want to replacefind
byfind_by
or you can also useparams[:id]
instead ofpoll_id
if that exists (depends on your routes).More information here on params: https://edgeguides.rubyonrails.org/action_controller_overview.html#parameters