Rails 4.0参数丢失或值为空 [英] Rails 4.0 param is missing or the value is empty
问题描述
我有一个带有嵌套资源链接的机会模型.在我的视图/机会/显示页面中,当我单击其中一个链接的"DestroY"时,出现错误:
I have an Opportunity model that has a nested resource Link. In my views/opportunities/show page when I click on "DestroY' for one of the links, I get the error:
参数丢失或值为空:链接
param is missing or the value is empty: link
它抱怨的代码段是:
def link_params
params.require(:link).permit(:description, :link_url)
end
这是我的销毁代码:
def destroy
@opportunity = Opportunity.find(params[:opportunity_id])
@link = @opportunity.links.find(link_params)
@link.destroy
respond_to do |format|
format.html { redirect_to links_url, notice: 'Link was successfully destroyed.' }
format.json { head :no_content }
end
推荐答案
更改此项:
@link = @opportunity.links.find(link_params)
对此:
@link = @opportunity.links.find(params[:id])
您的参数中没有link
,只有id
和opportunity_id
.
You don't have a link
in your params, you just have an id
and an opportunity_id
.
另外,你有这个:
respond_to do |format|
format.html { redirect_to links_url, notice: 'Link was successfully destroyed.' }
...
end
我猜您在opportunities
中嵌套了links
资源.因此没有links_url
.您需要使用opportunities_links_url(@opportunity)
.
I'm guessing you have your links
resource nested inside opportunities
. So there is no links_url
. You need to use, i.e., opportunities_links_url(@opportunity)
.
最后,请注意,除非您在此实例中明确需要绝对URL,否则可能需要opportunities_links_path
而不是opportunities_links_url
.
Finally, note that you probably want opportunities_links_path
rather than opportunities_links_url
unless you explicitly need absolute URLs in this instance.
您可以通过运行rake routes
找到链接帮助器.可以使用最后端的_url
或_path
调用最左边的"prefix"列中的所有内容以生成URL.
You can discover your link helper by running rake routes
. Everything in the leftmost "prefix" column can be called with _url
or _path
on the end to generate a url.
这篇关于Rails 4.0参数丢失或值为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!