1

こんにちは私は現在、ユーザーがアルバムを作成して写真をアップロードするためのサイトである、最初のRailsプロジェクトに取り組んでいます。登録、ログイン、フレンドリングをアプリにインストールしています。アルバム作成フォームで、友達のリストを表示して、アルバムへのアクセスを共有する相手を選択できるようにしようとしています(つまり、選択した相手も参加する@album.users予定です。チェックボックスを使用してこの選択を行うことについて(これ以上の方法は考えられません)。ただし、friendshipモデルをアルバム/新しいフォームにリンクする方法がわかりません。フォームは次のようになります。

album / new.html.erb

<%= form_for ([@user, @album]), :html => { :id => "uploadform", :multipart => true } do |f| %>
<div class="formholder">
    <%= f.label :name %>
    <%= f.text_field :name %>

    <%= f.check_box :friends %>

    <%= f.label :description %>
    <%= f.text_area :description %>

    <br>

    <%=f.submit %>
</div>
<% end %>

6行目でエラーが発生しました( <%= f.check_box :friends %>

エラー:

undefined method 'friends' for #<Album:0x007fa3a4a8abc0>

理由はわかりますが、修正方法がわかりません。私は友達を追加するための典型的な友情参加モデルを持っています、そして私はすべての友達のリストを見てそれらを選択できるようにしたいと思っています。次のステップは、アルバムコントローラーの作成アクションのようなものを追加することだと思いますが@album.users << @user.friendships.find_by_name(params[:friends])、友達に1つのパラメーターのみを返すフォームをループする方法がわかりません。

これが私のファイルです:

アルバムコントローラー作成アクション:

    def create
      @user = User.find(params[:user_id])
      @album = @user.albums.build(params[:album])
      # not so sure about the following line.
      @album.users << @user.friendships.find_by_name(params[:friends])
      respond_to do |format|
        if @user.save
          format.html { redirect_to user_album_path(@user, @album), notice: 'Album was successfully created.' }
          format.json { render json: @album, status: :created, location: @album}
        else
          format.html { render action: "new" }
          format.json { render json: @album.errors, status: :unprocessable_entity }
        end
      end 
    end

アルバムモデル

class Album < ActiveRecord::Base
  attr_accessible :name, :description
  validates_presence_of :name

  has_many :album_users
  has_many :users, :through => :album_user
  has_many :photos

end

ユーザーモデル

class User < ActiveRecord::Base

has_secure_password
attr_accessible :email, :name, :password, :password_confirmation
validates_presence_of :password, :on => :create

validates_format_of :name, :with => /[A-Za-z]+/, :on => :create
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
validates_length_of :password, :minimum => 5, :on => :create

has_many :album_users
has_many :albums, :through => :album_users
accepts_nested_attributes_for :albums

has_many :friendships
has_many :friends, :through => :friendships

before_save { |user| user.email = email.downcase }
before_save :create_remember_token

private

  def create_remember_token
    self.remember_token = SecureRandom.urlsafe_base64
  end

終わり

album_userモデル(テーブルに参加して、多くのユーザーがいるアルバムと多くのアルバムがあるユーザーの間で多対多の関係を作成します)

class AlbumUser < ActiveRecord::Base
  belongs_to :album
  belongs_to :user
end

友情モデル

class Friendship < ActiveRecord::Base
  attr_accessible :friend_id

  belongs_to :user
  belongs_to :friend, :class_name => "User"
end

さらに情報が必要な場合はお知らせください!! 前もって感謝します!!!

4

1 に答える 1

1

users_idsのアクセス可能な属性のリストに(はい、2つの「s」)を追加Albumしてから、フィールドで「複数選択」を使用する必要があり:users_idsます。

<%= f.collection_select(:users_ids, User.all, :id, :name, :multiple => true) %>
于 2012-10-06T06:45:24.390 に答える