2013-08-03 108 views
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 

回答

0
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在同一個控制器的方法。

+0

非常感謝。它是否甚至將'params [:comment] [:body]'傳遞給part_two? – MKK

+0

是的,'params [:comment] [:body]'也應該放在'part_two'中。讓我知道如果它不。 –

+0

如果part_one和part_two都是控制器操作,則這確實無法按預期工作。 – sevenseacat

2

您無法從其他操作調用操作。

從你的代碼示例,這聽起來像你的動作要part_two,但你part_one應該是過濾對於行動之前

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

+0

謝謝'part_one'將通過訪問'example.com/:mode /:id'來調用,但'part_two'將從Form調用。就我而言,我該怎麼做? – MKK

+1

爲什麼不能從另一個動作調用動作? –

+0

@sevenseacat應該將'part_two'放入application_controller中嗎? – MKK

相關問題