我已设置acts_as_follower,以便用户可以关注群组。到目前为止,这一切都有效,但我希望组的创建者(作者a.k.a the current_user)在创建时自动成为该组的追随者。
真的,有没有办法从群组控制器中执行另一个控制器操作? (。遵循)
class FollowsController < ApplicationController
def create
@group = Group.find(params[:group_id])
current_user.follow(@group)
respond_to do |format|
format.html { redirect_to @group }
format.js
end
end
def destroy
@group = Group.find(params[:group_id])
current_user.stop_following(@group)
respond_to do |format|
format.html { redirect_to @group }
format.js
end
end
end
groups_controller.rb
def create
@group = Group.new(group_params)
@group.author_id = current_user.id
if @group.save
flash.notice = "Group '#{@group.title}' Successfully Created!"
redirect_to group_path(@group)
else
flash.alert = "Group not created!"
render 'new'
end
end
答案 0 :(得分:0)
我回去想出来了。在模型保存后,您可以正常运行该方法:
groups_controller.rb
def create
@group = Group.new(group_params)
@group.author_id = current_user.id
if @group.save
flash.notice = "Group '#{@group.title}' Successfully Created!"
redirect_to group_path(@group)
@group.author.follow(@group)
else
flash.alert = "Group not created!"
render 'new'
end
end