0

User has_one Subscription

In config/routes.rb:

resources :users do
  member do
    resource :subscription, controller: :subscription
  end
end

I know that's probably wrong.

Form helper:

<%= simple_form_for [@user,@subscription] do |f| %>

But this format basically assumes a has_many relationship instead, and I get this error:

ActionView::Template::Error (undefined method `user_subscriptions_path' for #<#<Class:0x007f8066709020>:0x007f8066717670>):

Then I try this, which is closer:

<%= simple_form_for @subscription, html: { id: "new_subscription", class: 'form-horizontal' }, defaults: { label_html: { style: "float:left;" } } do |f| %>

But the result is:

ActionView::Template::Error (undefined method `subscriptions_path' for #<#<Class:0x007f8066709020>:0x007f806d57de20>):

Thanks for any help you can offer.

RubyRedGrapefruit
  • 12,066
  • 16
  • 92
  • 193

2 Answers2

1

It looks like you need to use the singular relationship in the simple_form plugin:

# User.rb
class User < ActiveRecord::Base
  has_one :subscription
  accepts_nested_attributes_for :subscription
end
Andy
  • 13,916
  • 1
  • 36
  • 78
  • That's not really what I'm going for Andy. My user is already created, and this is a Subscription page. If I did the signup all in one page I would be headed in this direction. I'm actually trying to break it up into discreet steps to make the screens simpler. But you have given me an idea on another problem I had, thank you for the help. Update: Please see the answer I just posted. – RubyRedGrapefruit Feb 04 '14 at 01:55
  • Ah, I understand. Is there a way to implement [`custom_form_for`][cff] and pass the User as a relationship? Sorry, it's been a few years since I've done active RoR development. [cff]: https://github.com/plataformatec/simple_form#custom-form-builder – Andy Feb 04 '14 at 02:01
1

The answer lies on SO right here. But don't look at the accepted answer, look at the extensively researched answer below it titled Where does that magic path come from?. That is the one that counts, and gives you an insight into the underpinnings of Rails at the same time.

Community
  • 1
  • 1
RubyRedGrapefruit
  • 12,066
  • 16
  • 92
  • 193