My current goal in my first rails project is to have a button that will create a @my_tea
using the attributes of a @tea
(show page). This is the error I am getting:
'undefined method `my_teas_path' for #<#:0xa578cf8>
I have tried having the form in a _new partial inside my_teas/ and inside teas/_add_tea both have given me the same error. Anyway here is my code as it stands. View:
<%= form_for([@user, @my_tea]) do |f| %>
<%= f.hidden_field :name, :value => @tea.name %>
<%= f.hidden_field :tea_type, :value => @tea.tea_type %>
<%= f.hidden_field :store, :value => @tea.store %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= fields_for [@user, @tea_relationship] do |r| %>
<%= r.hidden_field :tea_id, :value => @tea.id %>
<% end %>
<%= f.submit "Add Tea", class: "btn btn-large btn-primary" %>
<% end %>
my_tea controller
def new
@my_tea = MyTea.new
end
def show
@my_tea = MyTea.find(params[:id])
end
def create
@my_tea = MyTea.new(params[:my_tea])
if @my_tea.save
flash[:success] = "Tea added to your teas!"
else
redirect_to user_path
end
end
Teas controller:
def show
@tea = Tea.find(params[:id])
@my_tea = MyTea.new
@tea_relationship = TeaRelationship.new
end
Routes
resources :users do
resources :my_teas
end
resources :teas
Models:
class User < ActiveRecord::Base
has_many :my_teas, :dependent => :destroy
has_many :tea_relationships, :dependent => :destroy
class MyTea < ActiveRecord::Base
belongs_to :user
class TeaRelationship < ActiveRecord::Base
belongs_to :user, class_name: "User"
end
Tea model doesn't belong to anything.
Please help rails community your my only hope :p
Update changing my form to this
<%= form_for([@user, @my_tea]) do |f| %>
<%= f.hidden_field :name, :value => @tea.name %>
<%= f.hidden_field :tea_type, :value => @tea.tea_type %>
<%= f.hidden_field :store, :value => @tea.store %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= fields_for @tea_relationship do |r| %>
<%= r.hidden_field :tea_id, :value => @tea.id %>
<% end %>
<%= f.submit "Add Tea", class: "btn btn-large btn-primary" %>
<% end %>
it works and the @my_tea submits but the @tea_relationship doesn't.