开发者

Ruby Rails Routing error

Currently, I have a table of companies, and each company has a table to store their funding data with a date and a money value, I can create new data in rails console with

Fund.create :date_of_record=>"2010-01-02", :company_id=>"1", :money=>"2003"

when I go to the company page(e.g. company_id=1), I'm able to view the data I entered from console, and edit, update them, but when I click to add a new funds data I'm getting

No route matches {:controller=>"funds", :company_id=>#<Fund id: nil, date_of_record: nil, company_id: 1, money: nil, created_at: nil, updated_at: nil>}

my create funds from db:

class CreateFunds < ActiveRecord::Migration
  def change
    create_table :funds do |t|
      t.datetime :date_of_record
      t.references :company
      t.integer :money

      t.timestamps
    end
    add_index :funds, :company_id
  end
end

my funds/new.html:

<% form_for ([@company, @fund]) do |f| %>
    <p>
    <%= f.label :date_of_record %><br />
    <%= f.text_field :date_of_record %>
    </p>
    <p>
        <%= f.label :money %><br />
        <%= f.text_field :money %>
    </p>
    <p>
        <%= f.submit "Create" %>
    </p>
    <% end %>
<%= link_to 'Back', company_funds_path(@fund) %>

my funds_controller:

   def new
     @company = Company.find(params[:company_id])
     @fund = @company.funds.build
   end

   def create
     @company = Company.find(params[:company_id])

     @fund = @company.funds.build(params[:fund])
     if @fund.save
       redirect_to company_fund_url(@company, @fund)
     else
       render :action => "new"
     end
   end
etc..

my models/company.rb:

class Company < ActiveRecord::Base
  has_many :empnumbers
  has_many :funds 
end

my models/fund.rb:

class Fund < ActiveRecord::Base
  belongs_to :company
end

my routes.rb :

开发者_JAVA技巧
  resources :companies do
    resources :funds
  end

Thank you for you help!!


= link_to 'Back', company_funds_path(@fund)

Probably should be

= link_to 'Back', company_funds_path(@company)
# => /companies/:company_id/funds


Correct. You should do the same thing for adding funds to a given company. You pass @company to new_company_fund_path(@company)

<%= link_to 'add fund', new_company_fund_path(@company) %>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜