Search code examples
formsruby-on-rails-4dryslim-lang

In Rails 4 how do I add fields to a form in a DRY manner?


In my Rails 4 project I want to add 2 fields to all the forms for a controller on the server side.

I'd like to add this to all of my forms for a controller.

input name="authenticity_token" type="hidden" value='=form_authenticity_token'
input name="profilePage" type="hidden" value='#{params[:action]}'

Each view has one form.


Solution

  • You could make it a partial:

    #_csrf.html.slim
    input name="authenticity_token" type="hidden"value='=form_authenticity_token'
    input name="profilePage" type="hidden" value='#{params[:action]}'
    

    And then render it in each form:

    = render 'csrf'