ruby-on-railsrubytemplating

How to provide defaults value for partial parameters in Rails?


I need to render a shared partial which can receive a few parameters from several views, but I don't want to pass all parameters everytime. If I call the template without all parameters, I get an error.

Is there a way to define default values for parameters, only if they haven't been defined when calling render 'name_of_partial?


Solution

  • With Rails >=7.1 you can define strict locals with a magic comment:

    <%# locals: (my_param: "default value") -%>

    https://guides.rubyonrails.org/action_view_overview.html#strict-locals