Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make radio button checked by default based on different user role?

<% if role.name == "Administrator" %>
     <%= f.radio_button:status,'available', :checked => (params[:status] == nil ? true : params[:status]) %><label>Available</label>
     <%= f.radio_button:_status,'not available' %><label>Not Available</label>
<% else %>
     <%= f.radio_button:_status,'available' %><label>Available</label>
     <%= f.radio_button:_status,'not available' %><label>Not Available</label>
<% end %>

By default i want the available radio button to be checked in case of administrator and not available radio button for rest of user. But he can change it and when viewing for editing it should show the one he/she has selected and not the default one.

How can i do this? please help me.

like image 320
logesh Avatar asked Jul 02 '13 11:07

logesh


2 Answers

Try the following code.

<%= f.radio_button:_status,'available', :checked => (role.name == "Administrator") %><label>Available</label>
<%= f.radio_button:_status,'not available', :checked => (role.name != "Administrator") %><label>Not Available</label>
like image 112
Bachan Smruty Avatar answered Oct 18 '22 02:10

Bachan Smruty


If you take a look at the documentation for rails radio_button_tag you would see it accepts the following params:

radio_button_tag(name, value, checked = false, options = {})

So it would be enough the following code

<%= f.radio_button:_status,'available', role.name == "Administrator" %><label>Available</label>
<%= f.radio_button:_status,'not available', role.name != "Administrator" %><label>Not Available</label>

Without the need of adding a "checked" property that might result in an unwanted behaviour

like image 41
Rodrigo Garcia Avatar answered Oct 18 '22 02:10

Rodrigo Garcia