簡體   English   中英

Rails顯示belongs_to中的類別名稱

[英]Rails Displaying a category name from a belongs_to

我想顯示一個類別名稱,而不是來自屬於關系的數字(cat_id),我有汽車和制造,基本上這里是代碼 -

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

它通過make table-id和car table - make_id相關聯

謝謝

羅比

當然 - 屬於關系會給你一個對象(在你的情況下是一個Make ),你可以調用方法 - 包括獲取字段名稱!

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

class Car < ActiveRecord::Base
  belongs_to :make
end


class Make < ActiveRecord::Base

end

並且Make有一個名為name的字段,您可以在您的視圖中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM