On Mon, Aug 8, 2011 at 4:06 PM, Colin Law <[email protected]> wrote:

> On 8 August 2011 11:17, Sayuj Othayoth <[email protected]> wrote:
> > [...]
> > Please see my code. I am using devise as authentication controller.
> >
> > MODELS:
> >
> > class User < ActiveRecord::Base
> >   #has_and_belongs_to_many :roles
> >   has_many :status_messages
> >   # Include default devise modules. Others available are:
> >   # :token_authenticatable, :encryptable, :confirmable, :lockable,
> > :timeoutable and :omniauthable
> >   devise :database_authenticatable, :registerable,
> >          :recoverable, :rememberable, :trackable, :validatable
> >
> >   # Setup accessible (or protected) attributes for your model
> >   attr_accessible :email, :password, :password_confirmation, :remember_me
> >
> >   # def role?(role)
> >   #   return !!self.roles.find_by_name(role.to_s.camelize)
> >   # end
> > end
> >
> > class StatusMessage < ActiveRecord::Base
> >   belongs_to :users
>
> That should be :user, singular.
>
> >   #default_scope :order => "created_at DESC"
> > end
> >
> > CONTROLLER:
> >
> > class StatusMessageController < ApplicationController
> >   def index
> >     @status = StatusMessage.all
> >   end
> >
> >   def create
> >     @status = StatusMessage.new
> >     @status.user_id = current_user.id
> >     @status.status = params[:status]
> >     @status.save
> >     redirect_to :action => "index"
> >   end
> >
> > end
> >
> >
> > ERB:
> >
> > <h1>Home</h1>
> > <%= form_tag do |f| %>
> >   <%= text_area_tag :status %>
> >   <%= submit_tag "Submit" %>
> > <% end %>
> > <br />
> > <% @status.each do |s| %>
> >   <%= s.users.email %>
>
> That should be s.user.email.  Each message has just one user.
>
> Colin
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
> Colin,
When I use s.user.email, it shows:

NoMethodError in Status_message#index

Showing *
/home/sayuj/work/sayuj/microblog/app/views/status_message/index.html.erb*where
line
*#9* raised:

undefined method `user' for #<StatusMessage:0xb126384>

Extracted source (around line *#9*):

6: <% end %>
7: <br />
8: <% @status.each do |s| %>
9:   <%= s.user.email %>
10:   <%= s.status %> <br />
11: <% end %>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to