barusan dicoba, trnyata kl user dihapus langsung lwt database memang
menimbulkan error message itu. makasih infonya.
--
quote: while(not_succeed()) { try_harder(); }
Portfolio: http://kuntoaji.c0de.me
________________________________
Dari: feby <[email protected]>
Kepada: [email protected]
Terkirim: Kam, 29 Oktober, 2009 10:59:15
Judul: [id-ruby] Re: [ask] masalah pada ROR associations
Hi Mas,
Error: "The error occurred while evaluating nil.username"
pada saat code:
<%= post.user.username %></b></p>
itu bukan karena kesalahan relasi atau tidak ada foreign key yang turun. Karena
kalau kesalahan relasi errornya akan "undefined method or attribute... bla bla"
Error itu karena ada post yang pemiliknya ( user ) sudah dihapus.
Gampangnya seh itu tinggal tambahin:
<%= post.user.username if post.user %>
Bagusnya di cek table posts, liat dan pastikan bahwa user_id pada setiap row
ada di 'id' tabel users.
Itu itu sering terjadi karena kesalahan edit data yaitu menghapus user secara
langsung di database.
Ingat has_many :posts, :dependent => :destroy
tidak langsung membuat trigger untuk delete casecade di level db/mysql, tapi
hanya berlaku untuk level rails app.
Semoga membantu,
Best
Feby Artandi
--- In id-r...@yahoogroups .com, kunto aji <aji_o...@.. .> wrote:
>
> pagi!
>
> saya sdng brmasalah aplikasi ror saya, mohon bantuannya. Saya ingin
> menampilkan username, tp saya mendapat error message:
>
>
> Showing app/views/posts/ _post.html. erb where line #2 raised:
> You have a nil object when you didn't expect it!
> The error occurred while evaluating nil.usernameSebagai informasi, ini adalah
> isi dari beberapa file saya.
> Isi dari app/views/posts/ _post.html. erb:
>
> <% div_for post do %>
> <p><b>Posted <%= time_ago_in_ words(post. created_at) %> ago by <%=
> post.user.username %></b></p>
> <p><%= post.message %></p>
> <% end %>
>
> Isi dari app/models/post. rb:
>
> class Post < ActiveRecord: :Base
> belongs_to :user
> end
>
> Isi dari app/models/user. rb:
>
> class User < ActiveRecord: :Base
> has_many :posts, :dependent => :destroy
> ------------ --- cut ------------ -------
> end
>
> Isi dari app/controllers/ posts_controller .rb:
>
> class PostsController < ApplicationControll er
> def index
> if logged_in?
> @posts = Post.all(:order => 'Created_at DESC',
> :conditions => ['user_id = ?', current_user. id])
> else
> @posts = Post.all(:order => 'Created_at DESC')
> end
> respond_to do |format|
> format.html
> end
> end
> ------------ --------- -- cut ------------ --------- --------- -
> end
>
> Fyi, untuk user, saya generate pakai nifty-generators. saya udah di
> kutak-katik dari kemarin, tp blom ketemu solusinya sampai sekarang.
> makasih!
>
> --
> quote: while(not_succeed( )) { try_harder() ; }
> Portfolio: http://kuntoaji. c0de.me
>
>
> "Coba Yahoo! Mail baru yang LEBIH CEPAT. Rasakan bedanya sekarang!
> http://id.mail. yahoo.com& quot;
>
> [Non-text portions of this message have been removed]
>
Buat sendiri desain eksklusif Messenger Pingbox Anda sekarang! Membuat
tempat chat pribadi di blog Anda sekarang sangatlah mudah.
http://id.messenger.yahoo.com/pingbox/
[Non-text portions of this message have been removed]