3
HABTM에 문제가 있습니다. 나는 그것들을 볼 때 자습서를 따라 가고 있지만 운은 없다.확인란을 통한 HABTM 문제 레일 3
class Country < ActiveRecord::Base
has_and_belongs_to_many :videos
end
class Video < ActiveRecord::Base
has_and_belongs_to_many :countries
end
보기 편집 :
<% for country in Country.find(:all) %>
<%= check_box_tag "video[country_ids][]", country.id, @video.countries.include?(country.id) %>
<%= label_tag "video[country_ids][]", country.name, :for => "video[country_ids][]" %><br />
<% end %>
보기 표시 :
<% for country in @video.countries %>
<%= country.name %><br />
<% end %>
비디오 컨트롤러 :
def update
@video = Video.find(params[:id])
if @video.update_attributes(params[:video])
flash[:notice] = "Successfully updated video."
redirect_to video_url
else
render :action => 'edit'
end
end
그리고 로그의 오류 : 여기 내 코드는
Started POST "/videos/2" for 127.0.0.1 at Tue Feb 08 15:01:33 -0600 2011
Processing by VideosController#update as HTML
Parameters: {"commit"=>"Update Video", "authenticity_token"=>"6y01pmKxB+TEG0pbU6ujDsfwzqQW9eqiXBJfPHQW5+w=", "utf8"=>"✓", "id"=>"2", "video"=>{"name"=>"Video number 1", "vimeo_id"=>"12341234", "country_ids"=>["2", "3", "4", "11", "12", "13"]}}
[1m[36mVideo Load (0.4ms)[0m [1mSELECT "videos".* FROM "videos" WHERE ("videos"."id" = 2) LIMIT 1[0m
[1m[35mCountry Load (0.4ms)[0m SELECT "countries".* FROM "countries" WHERE ("countries"."id" IN (2, 3, 4, 11, 12, 13))
Completed in 35ms
ActiveRecord::StatementInvalid (Could not find table 'countries_videos'):
app/controllers/videos_controller.rb:31:in `update'
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (10.6ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (16.1ms)
비디오 테이블과 국가 테이블이 HABTM을 통해 참가하지 않습니다. countries_videos 테이블을 찾을 수 없다고합니다. 내가 뭘 놓치고 있니?
글쎄,'countries_videos' 테이블을 이전 시켰습니까? –
테이블에 HABTM 관계가 내포하는 관계에 대한 외래 키 설정이 있습니까? –