zyriuse
zyriuse

Reputation: 73

undefined method `Calendar' for 2:Fixnum

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

Answers (4)

zyriuse
zyriuse

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

shybovycha
shybovycha

Reputation: 12245

  1. your @user variable is set to user's ID, not user model instance
  2. you try to get the Calendar from @user, whilst calendar is the correct field name, defined at models/user.rb
  3. you try to get the calendar from the user id, not from the user

The solution is:

  1. replace line

    @user = current_user.id
    

    with

    @user = current_user
    
  2. replace line

    @calendar = @user.Calendar.new
    

    with

    @calendar = @user.calendar.new
    

Upvotes: 2

Joe Half Face
Joe Half Face

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

Marek Takac
Marek Takac

Reputation: 3048

Replace the line 21 of your calendars_controller.rb

@calendar = @user.Calendar.new

with

@calendar = @user.calendar.new

Upvotes: 0

Related Questions