0

Webサイトを構築してRubyonRailsを学ぼうとしています。ユーザーはコンテンツを追加できます:投稿と写真。投稿と写真には1つ以上のタグを付けることができます。それぞれの関係をHABTMにして、特定のタグを介して投稿や写真に簡単にアクセスできるようにします。その逆も可能です。

投稿や写真にタグを追加するためのフォームを作成しようとしています。どちらかのフォームを送信すると、次のエラーが発生します。

POST http://localhost:8080/post/1/tags 500 (Internal Server Error)

返されたオブジェクトを見ると(これは両方のフォームで同じです):

未定義のメソッド`post_id'for#Tag id:nil、name:" asdf"> on the line'@ tag.save'

タグのattr_accesibleにpost_id、picture_idを追加してみました。サイコロはありません。ご協力いただきありがとうございます!何か小さなものが足りないような気がします。

私のモデル:

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
4

2 に答える 2

1

を実行して出力されたルートを見てくださいrake routes。この行があります:

post_tags POST    /posts/:id/tags(.:format)    tags#tag_post

これは、パスが(単数posts)ではなく(複数)でなければならないことを示していますpost

したがって、代わりにに投稿する必要があります

/posts/1/tags

ちなみに、あなたはRailsを学んでいると言っていたので、タグ付けシステムを最初から構築しているのは素晴らしいことですが、後でこの素晴らしい宝石がタグ付け可能として機能することを確認することをお勧めします。とても簡単で楽しいです。そして、これが非常にうまくタグ付けを超えるレールキャストです。

于 2013-02-13T21:20:04.823 に答える
1

問題はにあります

  validates :name, :uniqueness =>  { :sscope => :post_id }
  validates :name, :uniqueness => { :scope => :picture_id }

これらのメソッドは、tagモデルがに格納されている属性を持っpost_idていることを期待しているためです。picture_idposts_tags

于 2013-02-13T21:25:36.023 に答える