Reputation: 73
I am trying to put a calendar on my user page. I added route to routes.rb
file. But I can not understand why it doesn't work.
route.rb
resources :users do
resources :calendars
end
The error is said to be at line 21
of calendars_controler.rb:
19 def new
20 @user = current_user.id
21 @calendar = @user.Calendar.new
22 end
application.erb
<h1> <% picture3 = image_tag("../images/twit.png", :border =>0, :class =>"smallh1", :class => "blue")%> </h1>
<%= link_to picture3, new_user_calendar_path(current_user.id) %>
models/user.rb
has_one :calendar
attr_accessible :calendar_id
I added the user_id
field to the calendar index page.
models/calendar.rb
attr_accessible :event, :published_on, :description, :user_id
belongs_to :user, :foreign_key => :user_id
Any help would be appreciated!
Upvotes: 0
Views: 1014
Reputation: 73
i have resolved my problem thanks to you guy
for resolve i made that
def new @user = current_user @calendar = @user.build_calendar end
when i use the association the function new do no exist it's build now
thanks again
Upvotes: 0
Reputation: 12245
@user
variable is set to user's ID, not user model instanceCalendar
from @user
, whilst calendar
is the correct field name, defined at models/user.rbcalendar
from the user id, not from the userThe solution is:
replace line
@user = current_user.id
with
@user = current_user
replace line
@calendar = @user.Calendar.new
with
@calendar = @user.calendar.new
Upvotes: 2
Reputation: 2333
def new
@user = current_user.id
@calendar = @user.Calendar.new
end
Here you are assigning @user
instant variable to Fixnum (user id), and that doesn't make sense.
@user = current_user
This is correct variant. And yes, capital letters in Ruby are for Class names and Module names (which are some kind of special classes)
Upvotes: 0
Reputation: 3048
Replace the line 21
of your calendars_controller.rb
@calendar = @user.Calendar.new
with
@calendar = @user.calendar.new
Upvotes: 0