【发布时间】:2012-01-20 01:17:56
【问题描述】:
我收到了帮助Setting up a polymorphic association
我现在无法实现提交表单和创建操作。用户只需要关注和取消关注每个模型。
在我的跟随控制器中
class FollowsController < ApplicationController
before_filter :find_supports
def create
@relation = find_supports
@follow = @relation.find(params[:follow])
current_user.follows.follow!(@follow)
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
def destroy
@follows = Follow.find(params[:id]).followable
current_user.unfollow!(@follows)
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
private
def find_supports
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
else
nil
end
end
end
end
在我的关注表单中,它在我的多态模型上呈现
编辑
<%= form_for @cause.follows.build(params[:follow]) do |f| %>
<div><%= f.hidden_field :followable_id %></div>
<div><%= f.hidden_field :followable_type %></div>
<div><%= f.hidden_field :follower_id %></div>
<div class="create-button"><%= f.submit "Follow" %></div>
<% end %> <!-- I now get the values for followable_id and followable_type, but it
wont get grab the follower_id and I get an error in the create action -->
在我的用户模型中,我有这 3 种方法
def following?(follow)
follows.find_by_followable_id(follow)
end
def follow!(followable)
follows.create![] ##I have tried many different params here
end
def unfollow!(followable)
follows.destroy(params[:followable_id]).destroy
end
我无法正确保存 follower_id、followable_id 和 followable_type。有什么建议么? 在此先感谢,我为此花了很多时间。
【问题讨论】:
-
你有没有想过这个问题?我在多态关联上创建时遇到了类似的问题。
-
@lflores 是的,我做到了。我将添加我的解决方案
标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1 polymorphic-associations