0

如何在同一个控制器中调用其他操作?
然后我想通过@community or @userparams[:comment][:body]part_two

我目前的代码是这样的。

def part_one
    if params[:mode] == "1"
        @community = Community.find(params[:id])
        @body = "This is for Community"
    elsif params[:mode] == "2"
        @user = User.find(params[:id])
        @body = "This is for User"
    end

    params[:comment][:body] = @body

    -----here I want to call action [part_two]-------
end


def part_two
    transaction that needs @community or @user,  and params[:comment][:body]
end
4

2 回答 2

3

你不能从另一个动作中调用一个动作。

从您的代码示例中,听起来您的操作应该是part_two,但您part_one应该是该操作的前置过滤器

http://guides.rubyonrails.org/action_controller_overview.html#filters

于 2013-08-03T03:40:20.020 回答
1
def part_one
    if params[:mode] == "1"
        @community = Community.find(params[:id])
        @body = "This is for Community"
    elsif params[:mode] == "2"
        @user = User.find(params[:id])
        @body = "This is for User"
    end

    params[:comment][:body] = @body

    # call part_two
    part_two
end

这将在同一个控制器中调用 part_two 方法。

于 2013-08-03T09:06:46.293 回答