I want to add the disabled option to f.radio_button depending on the condition

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 スクリーンショット 2020-09-20 17.07.05.jpg

When logged in スクリーンショット 2020-09-20 17.07.22.jpg

I was allowed to reference

I want to give the text_field a readonly option depending on the conditions

Recommended Posts

I want to add the disabled option to f.radio_button depending on the condition
I want to simplify the log output on Android
I want to add a delete function to the comment function
I want to use screen sharing on the login screen on Ubuntu 18
I want to put the JDK on my Mac PC
I want to add a browsing function with ruby on rails
I want to display background-ground-image on heroku.
I want to install PHP 7.2 on Ubuntu 20.04.
I wanted to add @VisibleForTesting to the method
I want to var_dump the contents of the intent
I want to truncate after the decimal point
I want to get the value in Ruby
I want to play a GIF image on the Andorid app (Java, Kotlin)
Rspec: I want to test the post-execution state when I set a method on subject
[Java] I want to calculate the difference from the date
I want to use NetBeans on Mac → I can use it!
I want to embed any TraceId in the log
I want to judge the range using the monthly degree
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
I want to dark mode with the SWT app
[Android Studio] I want to set restrictions on the values registered in EditText [Java]
[Android Studio] I want to use Maven library on Android
I want to place RadioButtons in the same RadioGroup at any position on the screen.
I want to call the main method using reflection
I want to add a reference type column later
[Rough commentary] I want to marry the pluck method
I want to hit the API with Rails on multiple docker-composes set up locally
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to use Java Applet easily on the command line without using an IDE
Implemented a strong API for "I want to display ~~ on the screen" with simple CQRS
[Beginner] I want to modify the migration file-How to use rollback-
I want to set the conditions to be displayed in collection_check_boxes
(´-`) .. oO (I want to easily find the standard output "Hello".
I want to expand the clickable part of the link_to method
I was addicted to looping the Update statement on MyBatis
I want to change the log output settings of UtilLoggingJdbcLogger
I want to call a method and count the number
I want to use the Java 8 DateTime API slowly (now)
I want to create a form to select the [Rails] category
I want to use the sanitize method other than View.
I want to give a class name to the select attribute
I want to recursively search the class list under the package
I want to distinct the duplicated data with has_many through
I want to transition to the same screen in the saved state
I want to narrow down the display of docker ps
I want to return multiple return values for the input argument
[Ruby] I want to reverse the order of the hash table
I want to temporarily disable the swipe gesture of UIPageViewController
I want to pass the startup command to postgres with docker-compose.
I want to simplify the conditional if-else statement in Java
I want to convert characters ...
I want to give edit and delete permissions only to the poster
I want to create a chat screen for the Swift chat app!
I want to connect SONY headphones WH-1000XM4 with LDAC on ubuntu 20.04! !!
I want to understand the flow of Spring processing request parameters
I want to use swipeback on a screen that uses XLPagerTabStrip
I just want to write Java using Eclipse on my Mac
I want to return to the previous screen with kotlin and java!
I opened the menu bar (option menu) on Android and saw it.