2012-03-10 70 views
0

我有一个rails网站,我想通过仪表板渲染实时网站。我为网站的后端创建了一个仪表板,但是当用户进行更改时,我希望他们先看到它。Rails在difnet控制器中呈现网站

这里是我的

Dashboard.rb

has_many :profiles, :dependent => :destroy 

has_many :blogs, :through => :profile, :dependent => :destroy 
has_many :videos, :through => :profile, :dependent => :destroy 
has_many :albums, :through => :profile, :dependent => :destroy 

仪表板的index.html

<%= render :partial => 'profiles/profile', :locals => {:profile => @profile} %> 

Profile.rb

belongs_to :dashboard 
has_many :blogs, :dependent => :destroy 
has_many :videos, :dependent => :destroy 
has_many :albums, :dependent => :destroy 

_profile.html.erb

<h1><%= @profile.name %></h1> 

<% album = @profile.albums.last %> 
<% if album.blank? %> 
<%= link_to 'Create a new album', new_album_path %></br> 
<% else %> 
<%= render :partial => 'albums/album', :locals => {:album => @profile.albums.last} %> 
<% end %> 

<% blog = @profile.blogs.last %> 
<% if blog.blank? %> 
<%= link_to 'Create a blog post', new_blog_path %><br/> 
<% else %> 
<%= render :partial => 'blogs/blog', :locals => {:blog => @profile.blogs.last}%> 
<% end %> 

<% video = @profile.videos.last %> 
<% if video.blank? %> 
<%= link_to 'Add new video', new_video_path %></br> 
<% else %> 
<%= render :partial => 'videos/video', :locals => {:video => @profile.videos.last} %> 
<% end %> 

当我通过前端观看的网站,但试图通过仪表盘我得到的错误进行查看时,上述工作正常

未定义零方法`名”:NilClass

这是profile.name线

如果我删除线以上,我得到了

未定义的方法'节目的零:NilClass

任何人有任何建议,我怎么可以解决这个问题吗?

+0

你可以发布仪表板控制器的'index'方法吗? – rjz 2012-03-10 17:52:29

+1

我想,你没有在你的索引方法中声明变量(@profile)。在控制台控制器中,def index @profile = SOMETHING.find(?)end – 2012-03-10 17:59:03

+0

No我没有在控制器中定义@profile,但是当我在索引I中定义了@profile = Profile.find(params [:id])'得到错误'无法找到没有ID的配置文件' – coletrain 2012-03-10 19:05:56

回答

1

需要找到@profile找到控制器内部。如果参数ID无效,则它将重定向到根URL,如果有效,它将呈现页面。

def index 
    @profile = Profile.find_by_id(params[:id]) 
    @profile || invalid_url! 
end 

private 
def invalid_url! 
    flash[:error] = 'URL is not valid !' 
    redirect_to root_url 
end 
1

您确定您已在仪表板控制器中定义@profile吗?看起来@profile是未定义的。

+0

不,我没有定义它,但是当我做了我收到一个错误。检查上面的错误评论。 – coletrain 2012-03-10 19:10:08