简体   繁体   English

HABTM通过Checkboxes问题Rails 3

[英]HABTM through Checkboxes Issue Rails 3

I've been having a problem HABTM. 我遇到了HABTM的问题。 I'm following tutorials as I see them but no luck. 我正在按照教程看,但没有运气。 Here's my code: 这是我的代码:

class Country < ActiveRecord::Base
  has_and_belongs_to_many :videos
end

class Video < ActiveRecord::Base
  has_and_belongs_to_many :countries
end

Edit View: 编辑视图:

<% 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 %>

Show View: 显示视图:

  <% for country in @video.countries %>
    <%= country.name %><br />
  <% end %>

Video Controller: 视频控制器:

  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

And the error in the log: 而日志中的错误:

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)

The Videos table and the Countries table are not joining thru the HABTM. 视频表和国家/地区表未通过HABTM加入。 It says it can't find the countries_videos table. 它说它找不到countries_videos表。 What am I missing? 我错过了什么?

Woops. Woops。 Ok so I didn't realize that I need to actually create those tables. 好的,所以我没有意识到我需要实际创建这些表。 Got it all working now. 现在全部工作了。

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM