How to suppress a error when `render` have a argument `nil` in Rails

ironsand

I have a render method in a view to call partial template like this:

<%= render @post if @post.present? %>

Is there a way to write like this?

<%= method_like_try(:render, @post) %>

So that I don't need to check the presence of @post.

If it is not implemented in Ruby or Rails, I'll just stick first style.

Simone Carletti

To be honest, there is nothing wrong with

<%= render @post if @post.present? %>

It's already concise. Any attempt to shorten it will effectively cause the opposite effect.

<%= method_like_try(:render, @post) %>

Is less readable, and hides the intent of the rendering. There are a couple of possible improvements I can suggest, instead of hiding the logic into that method.

  1. You can still reduce the line into

    <%= render @post if @post %>
    

    A nil object evaluates to false.

  2. The other improvements is to catch the fact that @post is nil before. For instance, where is @post coming from? If it's a show action, you can easily use a bang-method to raise an exception if the value is nil. Or you can use a before filter

    class MyController
    
      before_filter :find_post
    
      def show
        render @post
      end  
    
      def find_post
        @post = Post.find_in_someway
        @post or head(404)
      end
    end
    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Suppress an error when saving a record in Rails

From Dev

How to suppress tar error messages in when piping

From Dev

Rails error 'First argument in form cannot contain nil or be empty'

From Dev

form_for first argument nil error in Rails with defined associations

From Dev

Rails getting the error: First argument in form cannot contain nil or be empty

From Dev

How to solve argument error in Rails?

From Dev

How to render error messages in views in Rails?

From Dev

How to fully suppress an error when invoking powershell with a command?

From Dev

How to fully suppress an error when invoking powershell with a command?

From Dev

rails4: how to render error message when number has more scale digits than defined?

From Dev

Rails, Unexpected nil error

From Dev

Nil value: no issue when visiting edit view but causing error when fired 'render edit'

From Dev

How to suppress deprecation warning on annotation argument

From Dev

Double render error rails

From Dev

syntax error rails render

From Dev

Rails 4: Receiving the following error: First argument in form cannot contain nil or be empty

From Dev

Why am I getting ArgumentError when calling Rails tag helper with a nil argument?

From Dev

How to suppress error message of a command?

From Dev

How to suppress Undefined index error?

From Dev

How to suppress OpenCV error message

From Dev

How to suppress QXcbConnection: XCB error

From Dev

How to suppress error messages in zsh?

From Dev

How to apply where clause in Rails ActiveRecord when the value is not nil?

From Dev

how to understand First argument in form cannot contain nil or be empty in rails controller?

From Dev

First argument in form cannot contain nil or be empty on render

From Dev

Rails: nil error class not found

From Dev

Suppress Model Error in Sailsjs when testing

From Dev

How to suppress Rails's auto naming routes?

From Dev

How to render view in rails

Related Related

  1. 1

    Suppress an error when saving a record in Rails

  2. 2

    How to suppress tar error messages in when piping

  3. 3

    Rails error 'First argument in form cannot contain nil or be empty'

  4. 4

    form_for first argument nil error in Rails with defined associations

  5. 5

    Rails getting the error: First argument in form cannot contain nil or be empty

  6. 6

    How to solve argument error in Rails?

  7. 7

    How to render error messages in views in Rails?

  8. 8

    How to fully suppress an error when invoking powershell with a command?

  9. 9

    How to fully suppress an error when invoking powershell with a command?

  10. 10

    rails4: how to render error message when number has more scale digits than defined?

  11. 11

    Rails, Unexpected nil error

  12. 12

    Nil value: no issue when visiting edit view but causing error when fired 'render edit'

  13. 13

    How to suppress deprecation warning on annotation argument

  14. 14

    Double render error rails

  15. 15

    syntax error rails render

  16. 16

    Rails 4: Receiving the following error: First argument in form cannot contain nil or be empty

  17. 17

    Why am I getting ArgumentError when calling Rails tag helper with a nil argument?

  18. 18

    How to suppress error message of a command?

  19. 19

    How to suppress Undefined index error?

  20. 20

    How to suppress OpenCV error message

  21. 21

    How to suppress QXcbConnection: XCB error

  22. 22

    How to suppress error messages in zsh?

  23. 23

    How to apply where clause in Rails ActiveRecord when the value is not nil?

  24. 24

    how to understand First argument in form cannot contain nil or be empty in rails controller?

  25. 25

    First argument in form cannot contain nil or be empty on render

  26. 26

    Rails: nil error class not found

  27. 27

    Suppress Model Error in Sailsjs when testing

  28. 28

    How to suppress Rails's auto naming routes?

  29. 29

    How to render view in rails

HotTag

Archive