Reputation: 2302
Hi recently I have switched to rails and I'm stuck right now.
I created two scaffolds, connected them like this: clients got treatments, every client can have more treatments.
class Treatment < ActiveRecord::Base
belongs_to :client
end
in the Client model
class Client < ActiveRecord::Base
has_many :treatments
end
routes:
resources :clients do
resources :treatments
end
Prefix Verb URI Pattern Controller#Action
client_treatments GET /clients/:client_id/treatments(.:format) treatments#index
POST /clients/:client_id/treatments(.:format) treatments#create
new_client_treatment GET /clients/:client_id/treatments/new(.:format) treatments#new
edit_client_treatment GET /clients/:client_id/treatments/:id/edit(.:format) treatments#edit
client_treatment GET /clients/:client_id/treatments/:id(.:format) treatments#show
PATCH /clients/:client_id/treatments/:id(.:format) treatments#update
PUT /clients/:client_id/treatments/:id(.:format) treatments#update
DELETE /clients/:client_id/treatments/:id(.:format) treatments#destroy
clients GET /clients(.:format) clients#index
POST /clients(.:format) clients#create
new_client GET /clients/new(.:format) clients#new
edit_client GET /clients/:id/edit(.:format) clients#edit
client GET /clients/:id(.:format) clients#show
PATCH /clients/:id(.:format) clients#update
PUT /clients/:id(.:format) clients#update
DELETE /clients/:id(.:format) clients#destroy
root GET /
When i fire up rails console and input this:
c = Client.find(1)
c.treatments
I got results. My problem is, I can't figure out how to make a form for the client.treatments.
i tried this:
<%= form_for(@client.treatments) do |f| %>
<%= f.text_field "intervention" %>
<%= f.intervention %>
<% end %>
but failed. How do I have to set up my form for the @client.treatment and how do I have to set up my controller and which controller do I have to set up? I'm kinda lost here. Thanks for your time.
Upvotes: 2
Views: 1669
Reputation: 3083
try with this
<%= form_for([@client, @client.treatments.new]) do |f| %>
Upvotes: 2
Reputation: 30453
You may use some gem for that like awesome_nested_fields. Then it will be like this:
<%= form_for(@client) do |f| %>
<div class="items">
<%= f.nested_fields_for :treatments do |f| %>
...
<% end %>
</div>
<a href="#" class="add">add treatment</a>
<% end %>
Upvotes: 0