[英]Ruby on Rails routes.rb not working
我正在升級現有的Rails應用程序。 我想將新路由添加到route.rb文件,該文件鏈接到控制器中的函數。 從原來工作的routes.rb文件摘錄:
match 'items/:id/add_to_collection/:collection_pid' => 'items#add_to_collection', :via => [:get, :post]
控制器文件:
#add to a task
def add_to_collection
@item = Item.find(params[:id])
@collection_pid = params[:collection_pid]
#does the item have a RELS-EXT datastream?
@rels_ext_ds = RELS_EXT_Datastream.new(:id => params[:id])
if !@rels_ext_ds.nil?
@rels_ext_ds.add_relation("fedora:isMemberOfCollection",{"rdf:resource","info:fedora/" + @collection_pid})
@rels_ext_ds.save_to_fedora
end
render :text => "OK"
end
我想添加一個執行類似功能的函數,因此將其添加到routes.rb文件中:
match 'items/:id/add_to_event/:event_pid' => 'items#add_to_event', :via => [:get, :post]
並轉到控制器文件:
#add to an event
def add_to_event
@item = Item.find(params[:id])
@event_pid = params[:event_pid]
#does the item have a RELS-EXT datastream?
@rels_ext_ds = RELS_EXT_Datastream.new(:id => params[:id])
if !@rels_ext_ds.nil?
@rels_ext_ds.add_relation("fedora:isMemberOfEvent",{"rdf:resource","info:fedora/" + @event_pid})
@rels_ext_ds.save_to_fedora
end
render :text => "OK"
end
但是,當我運行該應用程序時(在重新啟動Apache之后),該應用程序將重定向並且不會調用該函數。 從日志文件:
Started GET "/app/items/123456/add_to_event/testerevent" for 192.168.102.22 at Tue Apr 08 15:42:18 +0200 2014
Processing by ItemsController#add_to_event as HTML
Parameters: {"id"=>"tcmp:123456", "event_pid"=>"testerevent"}
Redirected to https://localhost/app/login
Completed 302 Found in 87ms
我是否需要重新啟動其他服務,以某種方式重建應用程序或清除某些緩存? 我知道控制器中的函數內容是100%正確的,因為當我用新函數的內容替換原始函數的內容時,一切正常。 我試圖在我的新函數中打印一些日志,但是沒有找到日志,表明該函數從未被調用過。 它似乎在日志文件中匹配。 我在這里俯瞰什么嗎? 我對RoR相當陌生。 謝謝!
編輯:
我發現哪個功能將我的應用重定向到登錄頁面。 是在application_controller.rb文件中稱為check_authorization的函數。
def check_authorization
user = User.find(session['user'])
unless user.roles.detect{|role|
role.rights.detect{|right|
right.action == action_name && right.controller == self.class.controller_path
}
}
flash[:notice] = "Please log in to access the page you requested. (action "+action_name.to_s+", controller "+self.class.controller_path.to_s+")"
#request.env["HTTP_REFERER"] ? (redirect_to :back) : (redirect_to :controller => 'users',:action=>'login') <-- This function
end
奇怪的是,我確實已登錄。
首先,請檢查您使用或不喜歡康康的任何引擎。
請參閱日志文件"app/items/123456/add_to_event/testerevent"
它已被重定向,因此請更改路由並檢查是否正確或不知道,請嘗試一次
在路由前面添加“ /”。
match '/items/:id/add_to_event/:event_pid' => 'items#add_to_event', :via => [:get, :post]
我發現了問題。 它與路由文件或功能無關,這是用戶配置中未授予的某些權利。 無論如何,感謝您的努力!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.