<% 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.
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With