創建新的項目我有嵌套資源如下:問題在嵌套的資源
employer.rb
class Employer < ActiveRecord::Base
has_many :listings
end
listing.rb
class Listing < ActiveRecord::Base
belongs_to :employer
end
我都建成使用基本腳手架發電機。
除了當我爲僱主創建新的列表時,一切似乎都有效。這條路線是
new_employer_listing GET
/company/:employer_id/listings/new(.:format)
{:action=>"new", :controller=>"listings"}
當我瀏覽到新上市的URL(公司/ employer_id /上市/新)
我得到:
NoMethodError in ListingsController#new
undefined method `listing' for #<Employer:0x102dd5e48>
這裏是#NEW
的listings_controller碼def new
@employer = Employer.find_by_username(params[:employer_id])
@listing = @employer.listing.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @listing }
end
end
再次,其他一切工作(顯示,編輯等) - 我只是不能得到一個新的上市頁面上來.. 。 任何幫助都是極好的。
謝謝!
//編輯下面
def create
@employer = Employer.find_by_username(params[:employer_id])
@listing = @employer.listings.new(params[:listing])
respond_to do |format|
if @listing.save
format.html { redirect_to(@listing, :notice => 'Listing was successfully created.') }
format.xml { render :xml => @listing, :status => :created, :location => @listing }
else
format.html { render :action => "new" }
format.xml { render :xml => @listing.errors, :status => :unprocessable_entity }
end
end
end
錯誤:
No route matches {:action=>"show", :controller=>"listings", :id=>#<Listing id: 20, job_title: "asd", location: nil, status: nil, industry: nil, years: nil, degree_type: nil, degree_field: nil, employer_id: 1, employers_id: nil, user_id: nil>}
嗯謝謝 - 我「geting'沒有路由匹配{:controller =>」列表「}} - 任何其他建議?再次感謝! – stewart715
瞭解它 - 謝謝!它是一個窗體語法錯誤:x – stewart715
快速問題,如果你有時間:我用我的創建定義來編輯帖子,創建完成後重定向,但出現錯誤(上面也顯示)。感謝您幫助newb:D – stewart715