Ruby on Rails:在同一视图上执行2个操作

时间:2017-04-26 14:16:25

标签: ruby-on-rails ruby routing

我试图在一个视图上显示2个动作。

的routes.rb

resources :users do
 get "accepted",    to: "users#accepted"

users_controller.rb

def show
 @user = User.find(params[:id])
 @requests = @user.requests
end

def accepted
 @user = User.find(params[:id])
 @requests = Request.where(:driverid => @user.id).order("created_at DESC")
end

我试图将这两个动作指向用户视图页面。

我一直在玩部分人,看着类似的问题,但无法弄明白。任何帮助表示赞赏!

修改

show.html.haml

我希望以下内容为show动作:

%h2 Current requests:
- @user.requests.where(:accepted => 'f').each do |request|  
   etc

然后接受的行动:

- @requests.where(blah blah).each do |request|  
    etc

1 个答案:

答案 0 :(得分:1)

来自Rails documentation

def accepted
  @user = User.find(params[:id])
  @requests = Request.where(:driverid => @user.id).order("created_at DESC")
  render :show
end

编辑:

在控制器show动作中:

@requests = @user.requests.where(:accepted => 'f')

在节目视图中:

%h2 Current requests:
- @requests.each do |request|  
  # ...