以下是我的routes.rb示例 基本上我有用户有图片,可以评价其他人的图片
这根本不是非常RESTful,但我无法弄清楚如何扭转局面?
resources :users do
member do
post :reset_password
end
end
# to list images
get 'images/list/:order/:page/:per_page/:category', :to => 'images#list_filtered'
# to rate images
post 'images/rate', :to => 'images#rate'
# to list a user's images
get 'images/user/:user_id/:category/:order/:page/:per_page', :to => "images#user"
# to list users that rated an image
get 'users/rated/:image_id', :to => 'users#rated'
答案 0 :(得分:1)
为什么不将图像和评级视为资源?在他们的用户下嵌套图片并添加属于图片和用户的评级模型(用户给出评级,而不是评级图片的所有者)
resources :users do
resources :images do
resources :ratings, :only => [:create, :destroy]
end
post :reset_password, :on => :member
end
然后你可以像这样设置你的cotroller
# ratings_controller.rb
class RatingsController < ApplicationController
before_filter :find_image
# POST /users/26/images/351/ratings
def create
@image.ratings.create(params[:rating])
redirect_to image_path(@image), :notice => "You just rated this image"
end
# DELETE /users/26/images/351/ratings/35477
def destroy
@image.ratings.find(params[:id]).destroy!
redirect_to image_path(@image), :notice => "Deleted rating"
end
private
def find_image
@image = Image.where(:user_id => params[:user_id], :id => params[:image_id])
end
end
答案 1 :(得分:0)
以下是我推荐的几件事:
resources :users do member do post :reset_password end resources :images end resources :images do post :rate get :rated end
然后,您应该可以使用/ images查看图像。
您应该可以使用/ images /:id / rate来评估单个图像。
您应该能够使用/ images /:id / rated。
查看评分图像的用户您应该能够使用/ user /:id / images列出用户的图像。