Ruby 2.6.5 Rails 5.2.0 We are developing an app that has a function to search user posts from the map.
I wanted to disable some radio_buttons in form_with when the user wasn't logged in Initially, I wrote the conditional expression as follows.
erb:index.html.erb
<%= form_with url: map_request_path, method: :get do |f| %>
<%= f.radio_button :posts, "all_user", checked: true %>Posted by all users
<% if logged_in? %>
<%= f.radio_button :posts, "following", disabled: false %>Posts by yourself and the users you are following
<%= f.radio_button :posts, "current_user", disabled: false %>My post
<% else %>
<%= f.radio_button :posts, "following", disabled: true %>Posts by yourself and the users you are following
<%= f.radio_button :posts, "current_user", disabled: true %>My post
<% end %>
<%= f.submit 'View posted shops', class: "btn btn-primary" %>
<% end %>
<div id="map_index"></div>
<script>
~
~
~
initMap();
</script>
I was manipulating the disabled: part by condition with if logged_in ?, but it was redundant code. I wanted to do something with one line, so I looked it up and found it refreshed by doing the following.
erb:index.html.erb
<%= form_with url: map_request_path, method: :get do |f| %>
<%= f.radio_button :posts, "all_user", checked: true %>Posted by all users
<%= f.radio_button :posts, "following", disabled: current_user.nil? %>Posts by yourself and the users you are following
<%= f.radio_button :posts, "current_user", disabled: current_user.nil? %>My post
<%= f.submit 'View posted shops', class: "btn btn-primary" %>
<% end %>
<div id="map_index"></div>
<script>
~
~
~
initMap();
</script>
write disabled: current_user.nil? I was able to bring the boolean value of whether current_user is empty (whether I am logged in) to disabled :.
When not logged in
When logged in
I want to give the text_field a readonly option depending on the conditions
Recommended Posts