2013-01-14 70 views
0

我有這行代碼在我看來:嵌套的資源 - 第二個對象ID沒有被拉?

<%= 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 

如何得到它的工作任何幫助是非常讚賞。

謝謝!

更新 - 控制器

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 [email protected] 
    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']) %> 

isnitialized它太返回有效和合適的對象,如由上面的兩個對象的調試

+0

爲什麼要聲明兩次你的':d3heros'資源? –

+0

這樣我就可以擁有d3hero資源而不會被綁定到d3users –

+0

您的控制器代碼對於此操作看起來像什麼? –

回答

0

試試這個

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

嗯..我試過,並得到這個錯誤,而不是 - 未定義的方法'id'爲false:FalseClass –