Sometimes when building forms the rails way, you need to do some funky stuff to get your models prepped so they can be worked on in the controller.

For example, here I want to create a new subscription, but I need to first prep who the subscription is for, and what episode they want to subscribe to:

<%= form_for(current_user.subscriptions.build(episode_id: @episode.id)) do |f| %>
    <div><%= f.hidden_field :episode_id %></div>
    <%= f.submit "Secure Checkout", class: "btn btn-large btn-primary" %>
<% end %>

Which produces:

<form action="/subscriptions" id="new_subscription" method="post">

Then in my controller, if I really want the subscription to save itself I can:

current_user.subscribe!(@episode)

For a good example see

http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#sec-stats_and_a_follow_form

Advertisements