我不确定我的标题是否正确使用词汇表。我找到了一些人们有类似问题的SO帖子(我在这些帖子中找不到解决方案),所以我使用了类似的标题。我正在尝试允许用户创建俱乐部并自动将用户指定为俱乐部的成员。但是,当我试图建立俱乐部时,似乎我发现了一些无序的事情。
我有三种模式:
###Models###
#user
has_many :club_memberships, :class_name => 'ClubMembership', :foreign_key => :member_id, :primary_key => :id
has_many :clubs, :through => :club_memberships
#club
attr_accessor :club_name, :club_type
has_many :club_memberships
has_many :members, :through => :club_memberships
#clubmembership
attr_accessor :club_id, :member_id, :membership_type
belongs_to :club
belongs_to :member, :class_name => "User", :foreign_key => :member_id, :primary_key => :id
以下是控制器的相关部分
###Controllers###
#Clubs
def new
@club = Club.new
end
def create
@club = Club.new(club_params)
if @club.save
@club_membership = ClubMembership.create(
member_id: current_user.id,
club_id: @club.id,
membership_type: 'founder'
)
flash[:success] = "Club Created!"
redirect_to root_url
else
render @club.errors.full_messages
end
end
private
def club_params
params.require(:club).permit(:club_name, :club_type)
end
#ClubMemberships
def create
@club_membership = ClubMembership.new(club_membership_params)
if @club_membership.save
render @club_membership
else
render @club_membership.errors.full_messages
end
end
private
def club_membership_params
params.require(:club_membership).permit(:member_id, :club_id, :membership_type)
end
我的form_for
###View###
#club#new
= form_for(@club) do |f|
.field.center-align
= f.label :club_name
= f.text_field :club_name, :class => "form-control fieldbox", autofocus: true
.field.center-align
= f.label :club_type
= f.text_field :club_type, :class => 'form-control fieldbox', autofocus: true
.actions.center-align
= f.submit "Create Club!", :class => "btn hoverable padtop"
最后,这是日志在帖子上显示的内容
#log
Started POST "/clubs" for at 2015-09-03 22:32:41 +0000
Cannot render console from ! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ClubsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ar2dv41/Tqk9EVjwfLLeD8bnpLoVWQIdDxG3Ju1GO3stLLvPd/FFgoFF9YuHobWbgb2byqkgAMiWRJAg5YcGKQ==", "club"=>{"club_name"=>"Test Club", "club_type"=>"Test Type"}, "commit"=>"Create Club!"}
(0.2ms) BEGIN
Club Exists (0.4ms) SELECT 1 AS one FROM `clubs` WHERE `clubs`.`club_name` = BINARY 'Test Club' LIMIT 1
SQL (0.3ms) INSERT INTO `clubs` (`created_at`, `updated_at`) VALUES ('2015-09-03 22:32:41', '2015-09-03 22:32:41')
(3.4ms) COMMIT
User Load (0.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 56 LIMIT 1
(0.1ms) BEGIN
ClubMembership Exists (0.3ms) SELECT 1 AS one FROM `club_memberships` WHERE (`club_memberships`.`member_id` = BINARY 56 AND `club_memberships`.`club_id` IS NULL) LIMIT 1
SQL (1.6ms) INSERT INTO `club_memberships` (`created_at`, `updated_at`) VALUES ('2015-09-03 22:32:41', '2015-09-03 22:32:41')
(3.1ms) COMMIT
Redirected to c9
Completed 302 Found in 71ms (ActiveRecord: 11.1ms)
我会说实话。我不知道该POST发生了什么,或者从下一步解决这个问题。看起来我想要的参数正在经历,但事实并非如此。任何帮助将不胜感激。
答案 0 :(得分:0)
我明白了。我一次尝试了两件事,但它确实有效,所以我无法肯定地说有什么帮助,但我很确定第二件事是最重要的。
首先,我从两个型号(Club和ClubMembership)中删除了attr_accessor
。我为什么一开始就把它放在那里?不知道。我可能在某个地方看到它,并认为它很酷。
其次,在模型中,我还有一些我没有发布的验证器,因为我认为它们并不重要。我有:
validates :club_name, :club_type, :presence => true
validates :club_name, :uniqueness => true
我将其更改为:
validates :club_name, presence: true
validates :club_type, presence: true
原来这很重要,现在一切正常。