respond_with的用法

        随着学习的深入,Rails 3带给我的惊喜越来越多。respond_with实在是个好东西,最初是在一篇大牛的博客中看到这个东西。不过奇怪的是在api.rubyonrails.org中找不到它的说明。刚刚又看了一片关于respond_with的博客《respond_with in Rails 3》 ,写的很详细。

 

        以前用respond_with,感觉总是模模糊糊的,一直以为:location是一个类似前缀的东西。

 

        当看到“当数据保存成功后,它自动跳转到@story对应的显示页面,当保存失败后,会自动render的@story的new页面。”突然好像明白了,:location的值会不会就是成功要显示的页面?马上去实验了一下,果然!我在location的地方传了一个'http://google.com'进去,保存成功就看到Google了。

 

        怎么感觉自己越活越笨了呢,要费这么大事才能明白这么简单的东西。

++++++++++++++++++++++++++++++

     2011-03-16新增

++++++++++++++++++++++++++++++

        在实际的使用当中,常常会到namespace,如果按照前面说的方法,加一个:location,例如:

respond_with @order, :location => store_order_url(@order)

 希望订单创建成功后显示订单,当你像上面这样写的时候,会得到一个错误:

Routing Error

No route matches {:action=>"show", :id=>#<Order id: nil, ……>,  :controller => "store/orders"}

 查了好多资料,发现原来对于namespace有一种更方便的写法:

respond_with :store, @order

 如果,还想重定向到列表页,就这样写:

respond_with :store, @order, :location => store_orders_url

 恩,终于搞定了

 

 

# 智能遇敌系统(终极兼容版):优先未遇/未捕宝可梦 # 解决"method_defined? for nil:NilClass"错误,适配极旧框架 # 核心智能遇敌方法 def pbSmartEncounter # 基础校验 return false if $game_temp.in_battle || $game_map.interpreter.running? return false unless $PokemonEncounters && $PokemonEncounters.respond_to?(:encounter_tables) # 1. 获取当前地图遇敌表(旧框架兼容) current_map = $game_map.map_id encounter_table = $PokemonEncounters.encounter_tables[current_map] rescue nil return false unless encounter_table && !encounter_table.empty? # 2. 提取所有可能遇到的宝可梦 all_possible = [] EncounterTypes.each_key do |type| next unless encounter_table[type] encounter_table[type].each do |entry| species = entry[0] all_possible << species if species.is_a?(Integer) && species > 0 end end all_possible.uniq! return false if all_possible.empty? # 3. 筛选目标宝可梦 unseen = all_possible.reject { |s| $Trainer.seen[s] == true } unowned = all_possible.reject { |s| $Trainer.owned[s] == true } target_species = nil if !unseen.empty? target_species = unseen.sample pbMessage(_INTL("发现了从未见过的宝可梦!")) elsif !unowned.empty? target_species = unowned.sample pbMessage(_INTL("发现了尚未捕捉的宝可梦!")) else target_species = all_possible.sample pbMessage(_INTL("遭遇了野生宝可梦!")) end # 4. 获取随机等级 target_level = get_random_level(encounter_table, target_species) return false unless target_level # 5. 触发战斗 $PokemonTemp.encounterType = get_encounter_type(encounter_table, target_species) battle_result = pbWildBattle(target_species, target_level) $PokemonTemp.encounterType = -1 puts "[智能遇敌] 物种=#{target_species},等级=#{target_level}" battle_result end # 辅助方法:计算随机等级 def get_random_level(encounter_table, species) level_ranges = [] encounter_table.each_value do |entries| entries.each do |entry| # 兼容旧框架遇敌表格式(可能是[物种,等级]或[物种,min,max]) s = entry[0] min_lvl = entry[1].is_a?(Range) ? entry[1].min : entry[1] max_lvl = entry[1].is_a?(Range) ? entry[1].max : (entry[2] || entry[1]) if s == species && min_lvl.is_a?(Integer) && max_lvl.is_a?(Integer) level_ranges << (min_lvl..max_lvl) end end end return nil if level_ranges.empty? range = level_ranges.sample rand(range) end # 辅助方法:获取遇敌类型 def get_encounter_type(encounter_table, species) encounter_table.each do |type, entries| entries.each do |entry| return type if entry[0] == species end end EncounterTypes::Land end # 关键1:重写步数触发(绑定到Game_Player类,确保方法存在) class Game_Player # 检查原方法是否存在,存在则重写 if self.method_defined?(:pbStepTaken) alias_method :old_pbStepTaken, :pbStepTaken def pbStepTaken old_pbStepTaken # 执行原逻辑 # 触发智能遇敌 next if $game_temp.in_battle next unless $PokemonEncounters && $PokemonEncounters.respond_to?(:encounter_rate) if rand(100) < $PokemonEncounters.encounter_rate pbSmartEncounter end end else puts "[智能遇敌] 警告:Game_Player中未找到pbStepTaken,需手动检查脚本顺序" end end # 关键2:调试触发(绑定到Scene_Map类,明确上下文) class Scene_Map # 检查原update方法是否存在,避免重复定义 if self.method_defined?(:update) && !self.method_defined?(:update_with_encounter_debug) alias_method :update_with_encounter_debug, :update def update update_with_encounter_debug # 执行原更新逻辑 # F9强制触发遇敌(非战斗状态) if Input.press?(Input::F9) && !$game_temp.in_battle pbSmartEncounter end end end end
最新发布
10-31
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值