2014-01-11 50 views
0

在Rails的4 duvid方法set_x,因爲創建一個名爲 「get_json」如何忽略行動的Rails 4.0創建自定義操作

def get_json 

    @step_recipes = StepRecipe.all 
    respond_to do |format| 
     format.json render: @step_recipes 
    end 
    end 

自定義操作,並且將其添加到路線:

get "step_recipes/get_json" => "step_recipes#get_json" 

問題是白衣Rails中4回調,因爲我嘗試打開這條路線,並得到錯誤Couldn't find StepRecipe with id=get_json,並有這樣的:

Parameters: 

{"id"=>"get_json"} 

我想現在我怎麼忽略這個方法在控制器set_x,如何創建自定義操作,並沒有問題惠普呢?非常感謝。

回答

1

如果您在routes.rb中的文件看,你可能有這樣的地方,你已經添加了get_json定製路線上面的條目:

resources :step_recipes 

除其他,這增加了這相當於路線這樣的:

get "step_recipes/:id" => "step_recipes#show" 

由於這條路線的URL首先匹配,你必須讓你的get_json線,其將請求路由到#show行動的機會之前,並設置:id PARAM等於get_json

您需要將您的路線重命名爲其他名稱才能避免衝突。試試這個:

get "get_json/step_recipes" => "step_recipes#get_json" 

您需要確保添加自定義路線,當你不符合這些已經存在的任何其他路線。

相關問題