开发者

Rails create and update model Cant resolve paths

I cant understand why edit and new links doesn't work. I have controller named CargoController and have model Car. CargoController is:

  def new_auto

  end

  def edit_auto

  end
  def index
    @cars=Car.find_all_by_UserId(session[:user_id])
    if @cars.nil?
    end
  end

Car model is:

class Car < ActiveRecord::Base
  validates_presence_of     :TransportTypeId
  validates_presence_of     :CarModelId
  set_primary_key :CarId
  has_one :TransportType
  has_one :CarModel
  belongs_to :User
  has_many :PropertyOfCar, :dependent => :destroy
  has_many :CarProperty, :through => :PropertyOfCar
end

The View CargoController index.erb is:

<%if !@cars.blank?%>
    <table width="100%" border="0" cellpadding="0px" cellspacing=1px">
      <% @cars.each do |car|   %>
      <tr valign="top" class="<%= cycle('list-line-odd', 'list-line-even') %>">
        <td>
          <table border="0" width="100%" style="color:black">
            <tr>
              <td width="15%" align="left" valign="top" >Тип ТС:</td>
              <td width="70%" align="left" valign="top"><%= TransportType.find_by_TransportTypeId(car.TransportTypeId).Name %></td>
              <td align="right" valign="top" rowspan="2">
                <img alt=""  src="/images/Truck-icon.png"/>
              </td>
            </tr>
            <tr>
              <td width="15%" align="left" valign="top">Car model:</td>
              <td width="70%" align="left" valign="top"><%= CarModel.find_by_CarModelId(car.CarModelId).ModelName %></td>
            </tr>
            <tr>
              <td align="center" valign="top" colspan="3">
                  <label class="Chars"> Properties</label>
              </td>
            </tr>
            <tr colspan="3">
              <td>
                <%=  button_to 'Edit', :controller=>:cargo,:action=>:edit_auto,:id=>car.CarId %>
              </td>
            </tr>
          </table>
        </td>
      </tr>
    <% end %>
    </table>
  <% end %>

the view edit and new is :

<div class="center">
 <%= render 'form' %>
</div>

The template _form is:

<%= form_for(@car) do |form| %>
<table >
      <tr>
        <td>
          <label for="transport_type">Car Type:</label>
        </td>
        <td>
          <%= form.select(:TransportTypeId, TransportType.all.collect {|p| [ p.Name, p.TransportTypeId ] }, { :include_blank => 'Select type'},{:style=>'width: 200px'}) %>
        </td>
      </tr>
      <tr>
        <td>
          <label for="transport_type">Car model:</label>
        </td>
        <td>
          <%= form.select(:CarModelId, CarModel.all.collect {|p| [ p.ModelNa开发者_运维技巧me, p.CarModelId ] }, { :include_blank => 'Select model' },{:style=>'width: 200px'}) %>
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <%= button_to "add property",{:controller=>:cargo,:action=>:new_property},{:class =>"Button_style"} %>
        </td>
      </tr>
       <tr>
        <td colspan="2" align="left">
           <%= form.submit "Add car", :class => "submit" ,:class =>"Button_style"%>
        </td>
      </tr>
    </table>
<% end %>

So then i try to add or update car rails ask a car controller or write

ActionView::Template::Error (undefined method `model_name' for NilClass:Class):
    1: <%= form_for(@car) do |form| %>
    2: <table >
    3:       <tr>
    4:         <td>. 

What shall i do to make it work? In a routes i have

  get "cargo/index"

  get "cargo/new_auto"
match ':controller(/:action(/:id))' 


I think that you don't create instance variables for each actions.

def new_auto
  @car = Car.new
end

def edit_auto
  @car = Car.find(params[:id])
end

And you should write "resources :cars" routes.rb as follwing:

resources :cars

I think that you should be able to see the new_auto page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜