如何在同一控制器中调用其他操作?

时间:2013-08-03 03:35:54

标签: ruby-on-rails ruby-on-rails-3

如何在同一控制器中调用其他操作?
然后我想将@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

2 个答案:

答案 0 :(得分:3)

您无法通过其他操作调用操作。

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

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

答案 1 :(得分: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方法。