【问题标题】:How called view 'show' when id is in other controller当 id 在其他控制器中时如何调用视图“显示”
【发布时间】:2016-05-10 19:03:11
【问题描述】:

我有一个关于 index 再融资的链接

<%= link_to "View details", refinancing_path(@refinancing) %>

这个 refinancing_path(@refinancing) 例如是 refinancings/6

我的路线很简单:

resources :refinancings, except: :destroy

我的控制器是:

  def show
    @refinancing = Refinancing.find(params[:id])
    # THIS GET REFINANCING THROUGH HISTORIC REFINANCING
    @hist_refin = HistoricRefinancing.consult_historic_refinancing(params[:refinancing_id])
  end

这是历史性再融资的模型

class HistoricRefinancing < ActiveRecord::Base
  belongs_to :authorization
  has_many :refinancings

  scope :consult_historic_refinancing, -> (refinancing_id) { HistoricRefinancing.where("refinancing_id = ? ", "#{refinancing_id}")  }

我只需要在我的视图索引中单击链接_显示此授权的再融资详细信息。 我在点击时收到此错误:

No route matches {:action=>"show", :controller=>"refinancings", :id=>nil} missing required keys: [:id]

当然,没有 id,但是如何获得呢?请帮帮我!

更新#####################################

我的观点节目是……

<h3>Details of refinancing</h3>

<p>
  <strong>Name:</strong>
  <%= @refinancing.employee.person.name %>
</p>

<p>
  <strong>Period:</strong>
  <%= @refinancing.period.strftime("%m/%Y") %>
</p>

<p>
  <strong>Value</strong>
  <%= number_to_currency @refinancing.value %>
</p>

就这个……

【问题讨论】:

  • 您能详细更新一下您的观点吗?您定义视图的方式似乎有一个错误!
  • 听起来您可能正在尝试链接到未保存的对象(那些具有 nil id)
  • 使用 rake 路由正确检查路由

标签: ruby-on-rails ruby-on-rails-4 routes relationship rails-4-2-1


【解决方案1】:

正如您的评论,逻辑应该是:

<% authotization.historic_refinancing.refinancings.each do |refinancing| %>
  <%= link_to "View details", refinancing_path(refinancing) %>
<% end %>

【讨论】:

  • 对不起,我的代码是葡萄牙语,所以我翻译了它,写在这里我忘记了...但是我的路线是再融资和再融资控制器,错误是其他 =(
  • 我明白了,所以线索是:@refinancing 可能是 nil 。试试看?
  • 是的,是零……但我有 id
  • 问题是authorization没有refinancing_id和refinancing没有authorization_id,但是history_refinancing有authorization_id和refinancing_id
  • 这样就变得简单了,我更新一下我的答案,你可以看看
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-14
  • 1970-01-01
  • 1970-01-01
  • 2013-02-12
相关资源
最近更新 更多