Rails - 表单问题

发布于 2024-11-08 22:02:49 字数 2074 浏览 0 评论 0原文

在我的应用程序中,我有 3 个这样的模型:

class Person
has_many :groups
has_many :group_memberships, :foreign_key => "member_id"
end

class Group_Membership
 belongs_to :member, :class_name => 'Person'
 belongs_to :group
end

class Group
belongs_to :person
has_many :group_memberships
has_many :members, :class_name => "Person", :through => "group_memberships", :foreign_key => "member_id"

我需要的是在 group#show 页面中创建一个按钮,允许该人员请求 group_membership。根据上一个问题的建议,我在 Group_Membership 控制器上创建了一个 def create_membership

def create_membership
  @group = Group.find params[:id]
  if @group.group_memberships.create( :member_id => params[:member_id])
     redirect_to @group
  else
     render :action => 'show'
  end
end

所以我转到我的表单,在 group#show 视图上,我做了如下操作:

<%= form_for :group_membership, @group_membership, :url => { :action => "create_membership" } do |f| %>
<%= submit_tag "Create" %>
<% end %>

但是每次我单击按钮,它会将我重定向到创建新组的表单,而不是 Group_Membership。有什么提示吗? 提前致谢。

########编辑##########

好吧,我将我的代码更改为如下所示:

def create
@person = Person.find(current_person)
@group = Group.find(params[:group_id])
params[:group_membership] = {:member_id => @person.id, :group_id => @group.id}
@group_membership = Group_Membership.create(params[:group_membership])
if @group_membership.save
redirect_to @person
else
redirect_to @person
end
end

我的表单现在是这样的:

<% form_for(:group_membership, :url => group_memberships_path(:member_id => current_person, :group_id => @group.id)) do |f| %> <%=submit_tag“加入!” %>

现在我可以实际创建一个 group_membership 模型,但是数据库列 member_idgroup_id 中的两个 id 都添加为空白。

如果我将行 @group_membership = Group_Membership.create(params[:group_membership]) 更改为 @group_membership = Group.Group_Membership.create(params[:group_membership]),我能够保存组 ID。如果我更改为 Person.,情况也是如此。

In my app I have 3 models like that:

class Person
has_many :groups
has_many :group_memberships, :foreign_key => "member_id"
end

class Group_Membership
 belongs_to :member, :class_name => 'Person'
 belongs_to :group
end

class Group
belongs_to :person
has_many :group_memberships
has_many :members, :class_name => "Person", :through => "group_memberships", :foreign_key => "member_id"

What I need is to create a button in the group#show page, that allows the Person to ask for the group_membership. By suggestion from a previous question I made a def create_membership on my Group_Membership controller:

def create_membership
  @group = Group.find params[:id]
  if @group.group_memberships.create( :member_id => params[:member_id])
     redirect_to @group
  else
     render :action => 'show'
  end
end

So I moved on to my form, on the group#show view, and I did as follow:

<%= form_for :group_membership, @group_membership, :url => { :action => "create_membership" } do |f| %>
<%= submit_tag "Create" %>
<% end %>

But everytime I click on the button, it redirects me to the form to create a new Group, not Group_Membership. Any hints?
Thanks in advance.

########EDIT##########

Well, I kinda changed my code to something like this:

def create
@person = Person.find(current_person)
@group = Group.find(params[:group_id])
params[:group_membership] = {:member_id => @person.id, :group_id => @group.id}
@group_membership = Group_Membership.create(params[:group_membership])
if @group_membership.save
redirect_to @person
else
redirect_to @person
end
end

And my form is right now like that:

<% form_for(:group_membership, :url => group_memberships_path(:member_id => current_person, :group_id => @group.id)) do |f| %>
<%= submit_tag "Join!" %>

Now I'm able to actually create a group_membership model, but both id's are added blank in database column member_id and group_id.

If I change the line @group_membership = Group_Membership.create(params[:group_membership]) to @group_membership = Group.Group_Membership.create(params[:group_membership]), I'm able to get group id saved. Same goes if I change to Person..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乄_柒ぐ汐 2024-11-15 22:02:49

只是为了清理一下

def create
  @group_membership = Group_Membership.create(:group => Group.find(params[:group_id]), :member => Person.find(current_person))
  if @group_membership.save
    redirect_to @person
  else
    redirect_to @person
  end
end

这段代码应该在 Rails 3 中工作我不确定早期版本

just to clean up a bit

def create
  @group_membership = Group_Membership.create(:group => Group.find(params[:group_id]), :member => Person.find(current_person))
  if @group_membership.save
    redirect_to @person
  else
    redirect_to @person
  end
end

this code should work in rails 3 I'm not sure about earlier versions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文