1

我目前正在尝试制作一个用于上传图片的应用程序。有用户、相册、图片。问题是每个相册可以有多个所有者,因此在创建相册的表单中有一个复选框部分,您可以在其中突出显示您的朋友姓名并“邀请他们”成为所有者。但是,我正在努力做到这一点,以便在共享所有权之前必须“接受”邀请。我正在尝试使用连接表 ( album_user) 并使用albumand来执行此操作pending_album,它们:status在创建时以它们为特征

这是我的

Albums/new.html.erb(这里没有问题)

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

    <br>
    <label>Hosts</label>
    <% @user.friends.each do |friend| %>
    <%= friend.name %>
    <%= check_box_tag 'album[user_ids][]', friend.id, @album.users.include?(friend) %>
    <% end %>

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

    <br>

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

专辑控制器(此处有状态问题)

def create
  @user = User.find(params[:user_id])
  @album = @user.albums.build(params[:album].merge(:status => 'accepted'))
  @friends = @user.friends.find(params[:album][:user_ids])
  if @user.save
    for friend in @friends
      AlbumUser.create({:user_id => friend.id, :album_id => @album.id, :status => 'pending'})
      # friend.albums.build(params[:album].merge(:status => 'pending'))
    end
    redirect_to user_album_path(@user, @album), notice: 'Album was successfully created.'
  else
    render action: "new"
  end
end

用户模型

class User < ActiveRecord::Base

has_secure_password
attr_accessible :email, :name, :password, :password_confirmation, :profilepic
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
# validates :album, :uniqueness => true

has_many :album_users
has_many :albums, :through => :album_users, :conditions => {status: 'accepted'}
has_many :pending_albums, :through => :album_users, :source => :album, :conditions => {status: 'pending'}
accepts_nested_attributes_for :albums

has_many :friendships, :dependent => :destroy
has_many :friends, :through => :friendships, :conditions => "status = 'accepted'"
has_many :requested_friends, :through => :friendships, :source => :friend, :conditions => "status = 'requested'", :order => :created_at
has_many :pending_friends, :through => :friendships, :source => :friend, :conditions => "status = 'pending'", :order => :created_at

has_attached_file :profilepic

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

def name_with_initial
  "#{name}"
end

private

  def create_remember_token
    self.remember_token = SecureRandom.urlsafe_base64
  end

结尾

专辑模特

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

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

end

相册_用户模型

class AlbumUser < ActiveRecord::Base
  attr_accessible :album_id, :user_id, :status
  belongs_to :album
  belongs_to :user
end

在这里发布:users/show.html.erb(我在每个专辑都有一个图标,但似乎在创建专辑时,pending_album即使它们的 :status 是“待定”,它们也不会保存为 s。

    <% @user.pending_albums.each do |pending_album| %>
        <div class="albumthumb">
            <%= link_to image_tag(pending_album.photos ? pending_album.photos.first.avatar.url(:medium) : ("questionmark.png"), :size => "150x150"), user_album_path(@user, pending_album) %>
            <br>
            <%= pending_album.name %>
            <% if signed_in? %>
            <%= form_for(:album_user, :url => user_albums_path(:user_id => current_user.id, :album_id => pending_album.id)) do |f| %>
            <%= f.submit "Accept Invitation" %>
            <% end %>
            <% end %>
        </div>
    <% end %>
    <% @user.albums.each do |album| %>
        <div class="albumthumb">
            <%= link_to image_tag(!album.photos.empty? ? album.photos.first.avatar.url(:medium) : ("questionmark.png"), :size => "150x150"), user_album_path(@user, album) %>
            <br>
            <%= album.name %>
        </div>
    <% end %>

我的问题是专辑没有保存为pending_albums。我认为用户模型中的 :conditions 将有助于区分哪些是专辑,哪些是待处理的专辑?一切似乎都属于专辑,即使它们应该是待处理的。任何人都可以帮忙吗?

4

0 回答 0