2012-01-24 189 views
0

我正在爲滑板網站的客戶端進行多站點工作。到目前爲止,一切都很好,但我開始陷入整個部分的困境。我有一個網站和網站has_many:相冊(相冊也屬於網站),但是當我嘗試從網站上的網站主頁呈現相冊時,我得到了未定義的方法`model_name'爲NilClass:類Rails 3渲染局部外部視圖?

我試圖在網站/展示頁上呈現相冊/ _album.html.erb,以在網站的主頁上顯示網站最新的相冊。

相冊控制器

class AlbumsController < ApplicationController 

    def index 
    @albums = Album.all 
    end 

    def show 
    @album = Album.find(params[:id]) 
    end 

    def new 
    @album = Album.new 
    end 

    def edit 
    @album = Album.find(params[:id]) 
    end 

    def create 
    @album = current_site.albums.build(params[:album]) 

    if @album.save 
    redirect_to albums_path, :notice => 'Album was successfully created.' 
    end 
    end 

    def update 
    @album = Album.find(params[:id]) 

    if @album.update_attributes(params[:album]) 
    redirect_to album_path(@album), :notice => 'Album was successfully updated.' 
    end 
    end 

    def destroy 
    @album = Album.find(params[:id]) 
    @album.destroy 
    end 
end 

站點控制器

class SitesController < ApplicationController 

    def index 
    @sites = Site.all 
    end 

    def show 
    @site = Site.find_by_subdomain!(request.subdomain) 
    end 

    def new 
    @site = Site.new 
    end 

    def edit 
    @site = Site.find(params[:id]) 
    end 

    def create 
    @site = Site.new(params[:site]) 

    if @site.save 
    redirect_to @site, :notice => 'Signed up!' 
    end 
    end 

    def update 
    @site = Site.find(params[:id]) 

    if @site.update_attributes(params[:site]) 
     redirect_to @site, :notice => 'Site was successfully updated.' 
    end 
    end 

    def destroy 
    @site = Site.find(params[:id]) 
    @site.destroy 
    end 
end 

網站Show.html

<p id="notice"><%= notice %></p> 

<p> 
    <b>First name:</b> 
    <%= @site.first_name %> 
</p> 

<p> 
    <b>Last name:</b> 
    <%= @site.last_name %> 
</p> 

<p> 
    <b>Subdomain:</b> 
    <%= @site.subdomain %> 
</p> 

<%= render :partial => 'albums/album'%> 

<%= link_to 'Edit', edit_site_path(@site) %> | 
<%= link_to 'Back', sites_path %> 

相冊/ _album.html.erb

<%= div_for @album do %> 
    <h2><%= @album.title %></h2> 
    <%= image_tag @album.photo.url(:small) %> 
<% end %> 

我失去了我的專輯控制器的東西嗎?

回答

1

在你show.html,你需要專輯的收集傳遞給渲染方法

<%= render :partial => 'albums/album', :collection => @site.albums %> 

內_album.html.erb部分,你需要引用專輯屬性爲本地屬性,像這樣

<%= div_for album do %> 
    <h2><%= album.title %></h2> 
    ... 

您可以在此處詳細瞭解3.4.5 Rendering Collections

+0

諧音試過以上,但仍然沒有奏效。一切正常,除非我有'<%= @ album.title%>'rails說undefined方法'title' – coletrain

+1

實例屬性「@album」從未在SiteController中定義過,所以你不能使用它。嘗試使用本地屬性<%= album.title%> –

+0

YOU THE MAN!謝謝 :) – coletrain