Rails 3控制器忽略参数

时间:2011-06-13 22:11:57

标签: ruby-on-rails-3 activerecord parameter-passing

class TagController < ApplicationController

 def show
  @videos = Video.tagged_with(params[:id])

  respond_to do |format|
    format.html # show.html.erb
  end
 end

end

日志:

Started GET "/tag/node.js" for 127.0.0.1 at 2011-06-13 23:10:59 +0100
Processing by TagController#show as JS
Parameters: {"id"=>"node"}

目前我正在传递node.js作为params[:id]的值,但不知何故(根据日志)我的应用仅传递node作为参数值。

如何确保将值node.js传递给我的控制器?

提前致谢。

1 个答案:

答案 0 :(得分:2)

啊,路由中的(.:format)被Rails吃掉了; .js用于确定格式。

因此,对此的一个简单修复可能是:

respond_to do |format|
  format.html # show.html.erb
  format.js   { render <erb file used for html> }
end

但是,这并没有“感觉”正确,因为您使用format返回的内容不是那种格式(我们在请求.js时返回HTML)

如果可以,您应该将“node.js”更改为其他值,例如“node_js”。否则,请查看使用parse_query or parse_nested_query in Rack

或者您可以定义/重新定义路线,但不会将(.:format)添加到最后。