嵌套资源 - 第二个对象ID没有被拉出?

时间:2013-01-14 23:21:06

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2

我在视图中有这行代码:

<%= link_to(@a.heroes[i]['name'], edit_d3user_d3hero_path(@temp_d3user, @temp_d3hero)) %><br/>

我收到以下错误,我无法弄清楚原因

No route matches {:action=>"edit", :controller=>"d3heros", :d3user_id=>#<D3user id: 4, x_battleTag: "rwk-1242", x_timePLayed: nil, x_killMnstrTtl: 1014469, x_killsEliteTtl: 51552, x_lastHeroPlayed: "10692899", x_game_id: nil, created_at: "2013-01-14 13:10:35", updated_at: "2013-01-14 23:10:17">, :id=>false}

手动更改代码

<%= link_to(@a.heroes[i]['name'], edit_d3user_d3hero_path(@temp_d3user, 1)) %><br/>

有效,因此可以将问题隔离到@ temp_d3hero对象的id不被代码拉动。

@ temp_d3hero的调试

--- !ruby/object:D3hero
attributes:
  id: 1
  x_name: Ziyi
  x_class: wizard
  x_id: '10692899'
  x_level: 60
  ...

@ temp_d3user的调试

--- !ruby/object:D3user
attributes:
  id: 4
  x_battleTag: rwk-1242
  x_timePLayed: !!null 
  x_killMnstrTtl: 1014469

最后是我的模特

D3hero

class D3hero < ActiveRecord::Base
  attr_accessible :x_class, :x_name, :x_level, :x_life, :x_damage, :x_id , :x_gender

  validates :x_id, presence: true, uniqueness: true

  belongs_to :d3user
end

D3user

class D3user < ActiveRecord::Base
  attr_accessible :x_battleTag, :x_game_id, :x_killMnstrTtl, :x_killsEliteTtl, :x_lastHeroPlayed, :x_timePLayed

  has_many :d3heros
end

和routes.rb

D3gearcheck::Application.routes.draw do
  get "d3heros/new"

  get "d3users/new"

  resources :users
  resources :sessions, only: [:new, :create, :destroy]
  resources :heroines
  resources :d3users do
    resources :d3heros
  end
  resources :d3heros

非常感谢任何有关如何使其发挥作用的帮助。

谢谢!

UPDATE- controller

def resultv2
@mystring = params[:battleTag]
@mystring.gsub!("#", "-")  

@a = Covetous::Profile::Career.new @mystring
# after is interpret only if no exception before
flash.now[:success] = 'Battle Tag Lookup Successful'
@acount = @a.heroes.count

#### hack -- d3user not created. create it now. Otherwise, update now.

@d3user = D3user.find_by_x_battleTag(@mystring)
if !@d3user
  D3user.create(:x_battleTag => @mystring,
                :x_killMnstrTtl => @a.kills['monsters'],
                :x_killsEliteTtl => @a.kills['elites'],
                :x_lastHeroPlayed => @a.lastHeroPlayed) 
else
  @d3user.update_attributes(:x_battleTag => @mystring,
                           :x_killMnstrTtl => @a.kills['monsters'],
                           :x_killsEliteTtl => @a.kills['elites'],
                           :x_lastHeroPlayed => @a.lastHeroPlayed)     
end

此外,@ temp_d3hero和@ temp_d3user已通过

进行初始化
<% @temp_d3user = local_d3user?(params[:battleTag]) %>
<% @temp_d3hero = local_d3hero?(@a.heroes[i]['id']) %>

它也返回有效且正确的对象,如上面2个对象的调试所示

1 个答案:

答案 0 :(得分:0)

试试这个

<%= link_to(@a.heroes[i]['name'], edit_d3user_d3hero_path(@temp_d3user, @temp_d3hero.id)) %><br/>