简体   繁体   中英

Rails filtering partial results

I'm rendering a todo list for a user like so

<%= render @user.todos %>

What I'm trying to figure out is how to show only the ones that have been marked as completed.

Is this something that I should be doing in a controller or model, or is it ok to filter something like that in a view?

听起来像todo模型上的命名范围,它返回完成的待办事项,以便您可以这样做

<%= render @user.todos.completed %>

The logic should always go into your Model.

You should create a :scope (rails 3) or :default_scope (Rails 2) in your model and call it in the View directly. Something like completed_todos and then you can call @user.completed_todos. You simply set completed = true in the scope.

Hope this helps.

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