我想通過構建一個網站來學習Ruby on Rails。用戶可以添加內容:帖子和圖片。帖子和圖片可以有一個或多個標籤。我希望每個關係都是HABTM,這樣我就可以通過特定的標籤輕鬆訪問帖子,圖片,反之亦然。窗體爲兩個HABTM關係中的對象,Rails 3
我正在嘗試將表單添加到帖子和圖片的標籤。當我提交任何形式,我得到的錯誤是:
POST http://localhost:8080/post/1/tags 500 (Internal Server Error)
當我看到返回的對象(這是兩種形式相同):
未定義的方法`POST_ID」爲#Tag id:nil,name:「asdf」>上線'@ tag.save'
我已經嘗試添加post_id,picture_id到Tag的attr_accesible;沒有骰子。 感謝您的幫助!我覺得我只是缺少一些小東西。
我的模型:
class Tag < ActiveRecord::Base
attr_accessible :name
has_and_belongs_to_many :pictures
has_and_belongs_to_many :posts
validates :name, :presence => true
validates :name, :uniqueness => { :scope => :post_id }
validates :name, :uniqueness => { :scope => :picture_id }
end
class Post < ActiveRecord::Base
attr_accessible :content, :title
belongs_to :user
has_and_belongs_to_many :tags
end
class Picture < ActiveRecord::Base
attr_accessible :image, :name
belongs_to :user
has_and_belongs_to_many :tags
end
遷移:
class CreateTags < ActiveRecord::Migration
def change
create_table :tags do |t|
t.string :name
end
create_table :pictures_tags, :id => false do |t|
t.references :picture, :tag
end
end
end
class CreatePostsTags < ActiveRecord::Migration
def change
create_table :posts_tags, :id => false do |t|
t.references :post, :tag
end
end
end
在我看來
:
<%= form_for([@post, @post.tags.build]) do |f| %>
<%= f.label 'tag' %>
<%= f.text_area :name %>
<%= f.submit %>
<% end %>
<% current_user.pictures.each do |p|
<%= form_for([p, p.tags.build], :remote => true) do |f| %>
<%= f.text_area :name %>
<%= f.submit 'add tag' %>
<% end %>
<% end %>
在我TagsController
:
def tag_post
authenticate_user!
@post = Post.find(params[:id])
@tag = @post.tags.build(params[:tag])
@tag.save
redirect_to edit_post_path(@post)
end
def tag_picture
authenticate_user!
@picture = Picture.find(params[:id])
@tag = @state.picture.build(params[:tag])
@tag.save
redirect_to edit_picture_path(@picture)
end
的routes.rb:
post '/posts/:id/tags' => 'tags#tag_post', :as => :tag_post
post '/flow/:id/tags' => 'tags#tag_picture', :as => :tag_picture
耙路線:
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root / flow#flow
root / flow#home
posts_index GET /ramblin(.:format) posts#index
post_tags POST /posts/:id/tags(.:format) tags#tag_post
picture_tags POST /flow/:id/tags(.:format) tags#tag_picture
create_picture POST /flow(.:format) pictures#create
search POST /flow/search(.:format) flow#flow
post_id
和picture_id
屬性請運行'耙routes',給我們增加了輸出 – weltschmerz 2013-02-13 20:02:32耙路線在問題的底部。 – deakolt 2013-02-13 20:14:06