Search code examples
ruby-on-railsruby-on-rails-4simple-form

How to write simple_form for nested resources with a renamed path?


I have a Band model and a BandMember model. My routes definition is

resources :bands do
  resources :band_members, as: :members, path: 'members'
end

Now I want to make a simple form for BandMember like this:

<%= simple_form_for [@band, @band_member] do |f| %>
<% end %>

This throws an exception:

undefined method `band_band_member_path'

This would have worked if my model name was Member instead of BandMember. I do not wish to rename the model. Any ideas to get around this?


Solution

  • You can use the url option of the simple_form_for method:

    <%= simple_form_for [@band, @band_member], url: your_url_helper_path do |f| %>
      # ...
    <% end %>