简体   繁体   中英

Rails3 nomethod error #<ActiveRecord::Relation>


I'm writing a static page controller.
I get the menuname in the routes.rb and it's call the static controller show method.

match '/:menuname' => 'static#show'

And static_controller.rb:

@static=Staticpage.where("menuname = ?", params[:menuname])

But if I want print @static.title in the view, I get this error:

undefined method `title' for #

Whats wrong?

the SQL query looks good:

SELECT staticpages .* FROM staticpages WHERE (menuname = 'asd')

Couple of working alternatives:

@static = Staticpage.where("menuname = ?", params[:menuname]).first
@static = Staticpage.find_by_menuname(params[:menuname])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM