我正在开发一个学习平台,在这个阶段,用户,管理员和其他人都可以使用这些平台。 (带有学习内容的页面)。我想创建一个链接,将用户带到管理员创建的最新Feed。在控制台中,我可以使用' feed.last'。
找到此记录以下是我的申请中的路线:
feeds GET /feeds(.:format) feeds#index
POST /feeds(.:format) feeds#create
new_feed GET /feeds/new(.:format) feeds#new
edit_feed GET /feeds/:id/edit(.:format) feeds#edit
feed GET /feeds/:id(.:format) feeds#show
PATCH /feeds/:id(.:format) feeds#update
PUT /feeds/:id(.:format) feeds#update
DELETE /feeds/:id(.:format) feeds#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root GET / pages#home
有没有办法可以写一个' link_to'处理此问题的行动(例如:<%= link_to"最新Feed",___________%>)?
答案 0 :(得分:0)
There are 2 ways to do this. You could either add a member action to your feeds resource in routes.rb named latest (or whatever you like). Then you would create a corresponding controller action and view. In the controller action you would set @feed = Feed.last. The second option would be to link to the index or show action, add a GET parameter to the URL and then put logic in the controller action that returns Feed.last if the parameter is set.
答案 1 :(得分:0)
You could either make a separate controller action for this (to keep the URL constant), or just do this:
<%= link_to "Latest Feed", Feed.order(:created_at).last %>
Note that I've added .order
. This is to ensure that the it's the last one according to creation time, not the record's ID.