Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should not ask your User (model) to talk to the UI (view). That's bad/not MVC-clean. That's what a controller is for. </p> <p>You can use either return values, exceptions, or <code>throw/catch</code> (which is not exception handling) to pass information from your model to your controller. For example, using return values:</p> <pre><code>post "/create_user" do flash[:notice] = case User.something when User then "User Created!" when :nono then "That's not allowed" when :later then "User queued to be created later." end end class User def self.something if authorized if can_create_now new(...) else queue_create(...) :later end else :nono end end end </code></pre> <hr> <p>Since I mentioned them above, following are examples using <strong>throw/catch</strong> and <strong>begin/rescue</strong> (exceptions). As the advisability of using either of these constructs is questionable, let us take a moment of silence to ponder if this is a good idea.</p> <hr> <p>Here is an example using <strong>throw/catch</strong>:</p> <pre><code>post "/create_user" do result = catch(:msg){ User.something } flash[:notice] = case when :nono then "That's not allowed" when :later then "User queued to be created later." else "User Created!" end end class User def self.something throw :msg, :nono unless authorized if can_create_now new(...) else queue_create(...) throw :msg, :later end end end </code></pre> <p>Finally, here's an example using <strong>exceptions</strong>, though I'm not convinced that this will be appropriate for all (non-disastrous) cases where you want to flash unique messages to the user:</p> <pre><code>post "/create_user" do flash[:notice] = "User Created!" # Assume all good begin User.something rescue User::Trouble=&gt;e flash[:notice] = case e when Unauthorized then "That's not allowed" when DelayedCreate then "User queued to be created later." else "Uh...Something bad happened." end end end class User class Trouble &lt; RuntimeError; end class Unauthorized &lt; Trouble; end class DelayedCreate &lt; Trouble; end def self.something raise Unauthorized unless authorized if can_create_now new(...) else queue_create(...) raise DelayedCreate end end end </code></pre> <p>Exceptions let you pass an additional data along (e.g. <code>raise Unauthorized.new "No such account"</code>, or any custom properties you want to add to your class), and so may be more useful (when appropriate). Just remember to pass <em>semantic</em> results from your model to your controller, and let it translate them into <em>user-facing</em> messages.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload