首页 > link_to 带参数的问题

link_to 带参数的问题

在一个view里,@questions为一个由多个question组成的数组,我用link_to传参,在controller里读取的却是 #<ActiveRecord::Relation:0xde56b10>,请大家帮忙看看。

link_to "Try again",try_again_path(@question,:questions=>@questions),:class=> "btn btn-warning",:id=>"try-again", :style=>"margin-top:30px",remote:true

在controller里

@questions = params[:questions]
pp @questions

结果得到

“#<ActiveRecord::Relation:0xde56b10>"

大家帮忙看看,谢谢


在Rails里面 用where查出来的结果是一个 Relation 这个Relation有一些Array相同的方法比如说 each, collect, map, 等。

Question.where(:condition => "some value")
=> Relation

Question.where(:condition => "some value").all
=> Array

你在view里面用Relation 作为GET的参数 所以在Controller里面接收到的也是一个Relation 对象。

【热门文章】
【热门文章】