开发者

Rails 3 - collection_select - Understanding PROMPT?

开发者 https://www.devze.com 2023-01-20 05:05 出处:网络
I\'m buildi开发者_Python百科ng a form to allow a user to CRUD a project permission. .... <% roles = Role.all %>

I'm buildi开发者_Python百科ng a form to allow a user to CRUD a project permission.

....

<% roles = Role.all %>
<%= f.collection_select :role_id, roles, :id, :name, :prompt => true %>

Problems with the above, while it renders:

  1. If a value matches, it shows that in the dropdown as selected, which is good. Problem, is if a user is set as ADMIN. It's easy to use the dropdown to change the permission to something else, but not to CLEAR the permission...

Example... Select Drop Down: - Please Select - Admin - Member - Guest

If Admin is selected, Please Select never shows up.... How can I make an option show up to allow the user to remove the setting?

Any ideas? thx


I believe you want:

<%= f.collection_select(:role_id, roles, :id, :name, {:include_blank => 'Please Select'} %>

See the FormOptionsHelper docs for more information


<% roles = Role.all %>
<%= f.collection_select :role_id, roles, :id, :name, :prompt => (@user.admin? ? true : false) %>

does that help you?

you must be having a way to check if a user is admin / not.. use that condition in ternary operation to set the value of :prompt..

lemme know how it goes :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号