Rails Displaying a category name from a belongs_to

I would like to display a category name instead of a number (cat_id) from a belongs to relationship, I have cars and makes, basically here's the code -

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>

So basically i want the Make name here:-

<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

it is related through make table - id and car table - make_id

Thanks

Robbie

Answers


Sure - the belongs to relationship gives you an object (a Make in your case), which you can call methods on - including getting field names!

So, If you set up your models like so:

class Car < ActiveRecord::Base
  belongs_to :make
end


class Make < ActiveRecord::Base

end

And Make has a field named name, you could in your view:

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

Need Your Help

Detect entered character with JavaScript

javascript jquery events keyboard multilingual

I'd like to start an action (enabling autocomplete) when user types '@'. However I don't know how to do it correctly. I have jQuery available.

html onload not running javascript

javascript html function onload

I'm writing a game in javascript, and I had the world generator running fine, but when I created a startup function to run multiple things at once, my code doesn't run at all anymore. Can anyone se...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.