Ruby on Rails Switch

Ruby on Rails Switch

I assume you refer to case/when.

case a_variable # a_variable is the variable we want to compare
when 1 #compare to 1
puts "it was 1"
when 2 #compare to 2
puts "it was 2"
else
puts "it was something else"
end

or

puts case a_variable
when 1
"it was 1"
when 2
"it was 2"
else
"it was something else"
end

EDIT

Something that maybe not everyone knows about but what can be very useful is that you can use regexps in a case statement.

foo = "1Aheppsdf"

what = case foo
when /^[0-9]/
"Begins with a number"
when /^[a-zA-Z]/
"Begins with a letter"
else
"Begins with something else"
end
puts "String: #{what}"

How to write a switch statement in Ruby

Ruby uses the case expression instead.

case x
when 1..5
"It's between 1 and 5"
when 6
"It's 6"
when "foo", "bar"
"It's either foo or bar"
when String
"You passed a string"
else
"You gave me #{x} -- I have no idea what to do with that."
end

Ruby compares the object in the when clause with the object in the case clause using the === operator. For example, 1..5 === x, and not x === 1..5.

This allows for sophisticated when clauses as seen above. Ranges, classes and all sorts of things can be tested for rather than just equality.

Unlike switch statements in many other languages, Ruby’s case does not have fall-through, so there is no need to end each when with a break. You can also specify multiple matches in a single when clause like when "foo", "bar".

Ruby on Rails Case/Switch. How to match against object?

Make a user.status method which returns a state of a user, then you can do this:

user = Profile.find(1)
case user.status
when "banned"
redirect_to()
when "locked"
redirect_to()
else
redirect_to()
end

Rails switch case in the view

You should pull your first when into same block as case

<% @prods.each_with_index do |prod, index|%>
<% case index
when 0 %><%= image_tag prod.img, :id => "one") %>
<% when 1 %><%= image_tag prod.img, :id => "two") %>
<% when 2 %><%= image_tag prod.img, :id => "three") %>
<% end %>
<% end %>

Case statement with multiple values in each 'when' block

In a case statement, a , is the equivalent of || in an if statement.

case car
when 'toyota', 'lexus'
# code
end

Some other things you can do with a Ruby case statement

How to do a Switch based on controller with ruby on rails?

Quoting from http://rails.nuvvo.com/lesson/6371-action-controller-parameters:

The params hash will always contain the :controller and :action keys, but you should
use the methods controller_name and action_name instead to access these values. Any other parameters defined by the routing, such as :id will also be available.

So you should definitely be able to access it via params[:controller], and, if the controller_name method is in scope in a view, you should use that instead.

As for the switch syntax itself, you do need to do it like

case controller_name
when "home"
do_home
when "about"
do_about
else
do_default
end

You could do some hacking and get

case true
when controller "home"
do_home
when controller "about"
do_about
else
do_default
end

But why?

Rails - how can I make a profile switch on a radio button using a device and polymorphic associations?

This part of code

 <% if params[:profilable] == 'Client' %>
<% resource.set_client_profile %>
<% else %>
<% resource.set_realtor_profile %>
<% end %>

will be executed only on view rendering. The resource here is a an empty model that you use only for creating a form. This instance is not an actual model that will be created after the create request hits the server.
So, both set_client_profile and set_client_profile will be never called in create method of the controller. This code should be removed.

I guess the easiest way to do it is to redefine create method in Users::RegistrationsController. The original code of this method can be found in the repo.
So, it can be modified like this:

 def create
build_resource(sign_up_params)

#here comes your modification
if sign_up_params[:profilable] == 'Client'
resource.set_client_profile
else
resource.set_realtor_profile
end

resource.save
# After that, copy the rest of the original `create` method
...

How to create case(switch) statement with a hash of constants using ruby on rails?

It looks like you're just looking up a key in a hash and returning its value. The only caveat appears to be that if the ke isn't found then you want to return a default value of "IS". I would suggest using Hash#fetch for this.

class ApplicationController < ActionController::Base
VERBS = { pages: "IS",
apps: "MADE",
articles: "TAUGHT",
articles: "WONDERED"
}.stringify_keys.freeze

before_action :set_current_verb

private

def set_current_verb
@current_verb = VERBS.fetch(controller_name) { "IS" }
end
end

Note that I made VERBS a constant so that it would be visible to the set_current_verb method. And I included it above the private designation because constants can't be private anyway. Using the VERBS hash inside of the set_current_verb method would cause it to be evaluated every single time, so the constant is a better solution still. Also, controller_name is preferred over params[:controller] for its expressiveness and because it will avoid returning namespaces should any exist in the future.

Also, the VERBS hash seems to have two identical keys. I assume that's just a typo and can be corrected by you.



Related Topics



Leave a reply



Submit