2011-11-29 23 views
4

我想顯示一個類別名稱,而不是一個數字(CAT_ID)從所屬關係,我有車,使,基本上這裏的代碼 -滑軌從belongs_to的顯示類別名稱

show.html .erb

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


<p> 
    <b>Make:</b> 
    <%= @car.make_id %> 
</p> 

<h2> 
    <em><%= @car.model %> <%= @car.body_typw %> <%= @car.engine_size %> <%= @car.trim %></em> 
</h2> 

<p> 
    <%= image_tag @car.image(:large) %> 
</p> 

<% @carimages.each do |carimage| %> 

    <%= image_tag carimage.image(:thumb), :class => "imgsmall" %> 

<% end %> 

<p> 
    <b>Transmission:</b> 
    <%= @car.transmission %> 
</p> 

<p> 
    <b>Fuel type:</b> 
    <%= @car.fuel_type %> 
</p> 

<p> 
    <b>Millage:</b> 
    <%= @car.millage %> 
</p> 

<p> 
    <b>Price:</b> 
    <%= number_to_currency(@car.price) %> 
</p> 

<p> 
    <%= raw @car.content %> 
</p> 

所以基本上我想要的製作名稱的位置: -

<p> 
    <b>Make:</b> 
    <%= @car.make_id %> 
</p> 

cars_controller.rb

class CarsController < ApplicationController 
    # GET /cars 
    # GET /cars.json 
    def index 
    @cars = Car.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @cars } 
    end 
    end 

    # GET /cars/1 
    # GET /cars/1.json 
    def show 
    @car = Car.find(params[:id]) 
    @pages = Page.all 
    @carimages = Carimage.all 
    @carimages = Carimage.find(:all, :limit => 10, :order => "id DESC") 
    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @car } 
    end 
    end 

    # GET /cars/new 
    # GET /cars/new.json 
    def new 
    @car = Car.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @car } 
    end 
    end 

    # GET /cars/1/edit 
    def edit 
    @car = Car.find(params[:id]) 
    end 
    # POST /cars 
    # POST /cars.json 
    def create 
    @car = Car.new(params[:car]) 

    respond_to do |format| 
     if @car.save 
     format.html { redirect_to @car, notice: 'Car was successfully created.' } 
     format.json { render json: @car, status: :created, location: @car } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @car.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /cars/1 
    # PUT /cars/1.json 
    def update 
    @car = Car.find(params[:id]) 

    respond_to do |format| 
     if @car.update_attributes(params[:car]) 
     format.html { redirect_to @car, notice: 'Car was successfully updated.' } 
     format.json { head :ok } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @car.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /cars/1 
    # DELETE /cars/1.json 
    def destroy 
    @car = Car.find(params[:id]) 
    @car.destroy 

    respond_to do |format| 
     format.html { redirect_to cars_url } 
     format.json { head :ok } 
    end 
    end 
end 

它是通過化妝臺相關 - ID號,車型表 - make_id

感謝

羅比

回答

4

當然 - 在所屬關係,爲您提供了一個對象(在你的情況下Make)您可以調用方法 - 包括獲取字段名稱!

所以,如果你設置你的模型像這樣:

class Car < ActiveRecord::Base 
    belongs_to :make 
end 


class Make < ActiveRecord::Base 

end 

而且Make有一個字段命名爲name,您可以在您的視圖:

<p> 
    <b>Make:</b> 
    <%= @car.make.name %> 
</p> 
+0

注意,許多人可能認爲這是一個違反德米特法,但其重要性取決於開發者。 –

+0

沒有意識到這是那麼容易,謝謝;) –

+0

是的,我想過提到類似的東西(Rails給出的委託方法是有趣的閱讀有時),但決定反對它(不要把這個問題複雜化到一個Rails初學者):) – RyanWilcox

相關問題