ruby on rails -嵌套资源-第二个对象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 !@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个对象的调试所示

试试这个

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

相关内容

  • 没有找到相关文章

最新更新