Rails 4 / ajax – NoMethodError – nil的未定义方法`id’:NilClass – 将变量传递给js模态
我有一个非常简单的Ruby On Rails应用程序,前端有bootstrap 3:我有交易,每个交易都有很多机会。
在每个交易页面(交易页面的url是例如myapp.com/id=1),我希望有一个文本链接,显示“查看交易的机会”,单击时,它会在单击Bootstrap中的内容时触发模态。
我搞得一团糟,因为在这个Deal Page上,我使用了Deal的控制器动作“showcase”。 但是要通过ajax生成模态,如下所示,我使用另一个名为“show_opportunities”的动作,在其中,应用程序似乎不知道@deal(因为@ deal是在不同的Deal的动作中定义的)。 我认为问题出现的地方,但我不完全确定。
这是我得到的错误和我的代码:
Completed 404 Not Found in 3ms ActiveRecord::RecordNotFound - Couldn't find Deal without an ID: activerecord (4.2.0) lib/active_record/relation/finder_methods.rb:433:in `find_with_ids' activerecord (4.2.0) lib/active_record/relation/finder_methods.rb:71:in `find' friendly_id (5.1.0) lib/friendly_id/finder_methods.rb:20:in `find' app/controllers/deals_controller.rb:70:in `show_opportunities'
这行70,错误是“@deal = Deal.friendly.find(params [:id])”(见下文)
controller/deals_controller.rb (using @Pavan feedback) # used so that old urls created for deals redirects to the new ones created # indeed with friendly_id, the url is taken from the title :/deals/title # but if we edit the deal title to deal/title2, the old url was still working # we need to redirect the old url # source - github.com/norman/friendly_id/issues/385 before_filter :find_deal, :only => [ :showcase ] before_filter :ensure_canonical_deal_path!, :only => [ :showcase ] def showcase @deal = Deal.friendly.find(params[:id]) respond_to do |format| format.html # showcase.html.erb format.json { render json: @deal } end end def show_opportunities @deal = Deal.friendly.find(params[:deal]) @opportunity = Opportunity.where('deal_id = ? AND deal_type = ?', @deal.id, "high tech").first respond_to do |format| format.js end end protected def find_deal @deal = Deal.friendly.find params[:id] end def ensure_canonical_deal_path! if request.path != deal_page_path(@deal) redirect_to deal_page_path(@deal, :format => params[:format]), :status => :moved_permanently return false end end
应用程序/视图/交易/ showcase.html.erb
this is the beginning this is the end
意见/交易/ _deal_info_zone.html.erb
意见/交易/ deal_opportunities_modal.js.erb
这是通过ajax:views / deals / opportunity_modal的模态触发器。 请注意我在这里试图通过交易,但没有成功,所以该行
$('body').append(''); $('#myModal').modal('show');
/app/views/deals/_deal_opportunities_modal.html.erb
的routes.rb
match '/deals/:id', # this is the Deal page to: 'deals#showcase', via: 'get', as: :deal_page match '/opportunity_modal', to: 'deals#show_opportunities', via: 'get', as: :deal_opportunities_modal
如何使这项工作?
用Pavan的反馈编辑我的问题
解
我终于找到了我的错误:
我所做的是错的:当我在链接’查看机会’上方徘徊时,我可以看到链接指向的URL是localhost:3000 / opportunity_modal。 但是多么不可能! 我不能给所有不同的交易页面(id = 1,id = 2,…)中显示的所有链接“查看机会”提供相同的url。
所以我改变了路线:
match '/deals/:id/opportunity_modal', to: 'deals#show_opportunities', via: 'get', as: :deal_opportunities_modal
它的工作原理!!!
NoMethodError – nil的未定义方法`id’:NilClass
@deal
为nil ,您需要在show_opportunities
方法中定义它。
def show_opportunities @deal = Deal.friendly.find(params[:id]) @opportunity = Opportunity.where('deal_id = ? AND deal_type = ?', @deal.id, "high tech").first respond_to do |format| format.js end end
更新:
首先,在showcase
方法deals = Deal.friendly.find(params[:id])
更改为@deal = Deal.friendly.find(params[:id])
。
并将其作为locals
传递给<%= render 'deals/deal_info_zone' %>
<%= render 'deals/deal_info_zone', locals:{deal: @deal} %>
,然后将其传递给_deal_info_zone.html.erb
link_to
<%= link_to "view infos of Opportunities", deal_opportunities_modal_path, deal: @deal, remote: true %>
最后在show_opportunities
方法中访问它,如@deal = Deal.friendly.find(params[:deal])