Reputation: 974
I'm new to Ruby on Rails and i didn't get my code working with the array and hashes combination.
I have view with a @ftp_accounts[p.id].each:
- current_user.packets.each do |p|
.row
.widget.col-6
.widget-header
= fa_icon 'info'
FTP-Accounts für das Paket: #{ p.name }
.widget-body
.widget-title
Sie haben ##USED_FTP_ACCOUNTS## von #{ p.ftp_accs } genutzt.
%p
Um einen FTP-Account Ihrem Paket hinzuzufügen, klicken Sie bitte
%a{ href:'' }>hier
\.
%table
%thead
%tr
%th #
%th Benutzer
%th Kommentar
%th Verzeichnis
%th Quota
%th Aktion
%tbody
- if @ftp_accounts[p.id].nil?
%tr
- else
- @ftp_accounts[p.id].each do |ftp_acc|
%tr
%td
= ftp_acc.id
%td
= ftp_acc.username
%td
= ftp_acc.comment
%td
= ftp_acc.dir
%td
= number_to_human_size(ftp_acc.quota_size)
%td asd
With - @ftp_accounts[p.id].each do |ftp_acc|
I want to display all entries from my @ftp_accounts[p.id] array.
I get with = @ftp_accounts[p.id].inspect
with this output:
{2=>#<FtpAccount id: 2, username: "example_2", status: 1, password: "88ea39439e74fa27c09a4fc0bc8ebe6d00978392", uid: 2000, gid: 2000, dir: "/home/test", ul_bandwidth: 100, dl_bandwidth: 100, comment: "Jop", ipaccess: "*", quota_size: 20, quota_files: 0, user_id: 1, packet_id: 1, created_at: nil, updated_at: nil>}
and I want my two ftp-accounts:
#<ActiveRecord::Associations::CollectionProxy [#<FtpAccount id: 1, username: "example", status: 1, password: "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", uid: 2000, gid: 2000, dir: "/home/customer/ftp60047", ul_bandwidth: 500, dl_bandwidth: 500, comment: "SEEDS ACCOUNT 2GB", ipaccess: "*", quota_size: 2048, quota_files: 0, user_id: 1, packet_id: 1, created_at: "2015-05-27 15:34:36", updated_at: "2015-05-27 15:34:36">, #<FtpAccount id: 2, username: "example_2", status: 1, password: "88ea39439e74fa27c09a4fc0bc8ebe6d00978392", uid: 2000, gid: 2000, dir: "/home/test", ul_bandwidth: 100, dl_bandwidth: 100, comment: "Jop", ipaccess: "*", quota_size: 20, quota_files: 0, user_id: 1, packet_id: 1, created_at: nil, updated_at: nil>]>
My Controller:
def index
@ftp_accounts = Hash.new
current_user.ftp_accounts.to_ary.each do |ftp_acc|
@ftp_accounts[ftp_acc.packet_id] = Hash.new
@ftp_accounts[ftp_acc.packet_id][ftp_acc.id] = Hash.new
@ftp_accounts[ftp_acc.packet_id][ftp_acc.id] = ftp_acc
end
end
My user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
has_and_belongs_to_many :packets, :join_table => :users_packets, dependent: :destroy
# BackUp
has_many :backups, dependent: :destroy
has_many :ftp_accounts, dependent: :destroy
end
and my ftp_account.rb
class FtpAccount < ActiveRecord::Base
belongs_to :user
belongs_to :packet
end
and my packet.rb
class Packet < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :users_packets
belongs_to :resource, :polymorphic => true
belongs_to :user
end
Upvotes: 0
Views: 655
Reputation:
Your controller logic is the problem:
def index
@ftp_accounts = Hash.new
current_user.ftp_accounts.to_ary.each do |ftp_acc|
@ftp_accounts[ftp_acc.packet_id] = Hash.new # <-- this line is a problem
@ftp_accounts[ftp_acc.packet_id][ftp_acc.id] = Hash.new
@ftp_accounts[ftp_acc.packet_id][ftp_acc.id] = ftp_acc
end
end
The first line inside the loop will wipe out everything that was put into the variable in previous iterations if they share the same packet_id
.
You can change this to:
@ftp_accounts[ftp_acc.packet_id] ||= Hash.new
Note: the second line inside the loop does nothing, because you're setting a value to a new Hash, then immediately overwriting it with ftp_acc
.
Upvotes: 1