我对Rails很陌生,对我的问题所在也不了解。几天前才刚开始,现在我正在尝试向特定帖子添加评论。嗯,从技术上讲,这些不是注释,而是当其他人与该请求进行交互时,例如请求A具有日志B,日志C,日志D的更多日志添加到了某个请求,因此它基本上与注释系统相同。
我已经可以创建日志并显示它们了,但是我的主要问题是为正确的请求显示正确的日志。我的日志已经有正确的请求ID,但我不知道如何获取特定请求的特定日志
这是我的historys_controller.rb
class HistorysController < ActionController::Base
before_action :require_login
def index
@historys = History.where(job_order: 1)
end
def require_login
unless session['user_credentials_id']
redirect_to '/'
end
end
end
它仍设置为1作为临时占位符,并尝试类似
History.where(job_order: @job_order.id)
和
History.where(job_order: params [:id])
不起作用,那我应该放在那儿吗?
编辑
history.rb
class History < ApplicationRecord
belongs_to :job_order, polymorphic: true
belongs_to :actor, class_name: "User"
end
routes.rb
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'user_sessions#new'
get '/sign_in', to: 'user_sessions#new', as: :sign_in
get '/signup' => 'users#signup', as: 'signup_user'
delete '/sign_out', to: 'user_sessions#destroy', as: :sign_out
post '/register' => 'users#register', as: 'register_user'
get '/users/activate/:id' => 'users#activate', as: 'activate_user'
get '/users/deactivate/:id' => 'users#deactivate', as: 'deactivate_user'
get '/job_order_tracking_system' => 'job_orders#dashboard', as: 'job_order_tracking_system'
get '/job_orders/approve_job_order/:id' => 'job_orders#approve_job_order', as: 'approve_job_order'
get '/job_orders/reject_job_order/:id' => 'job_orders#reject_job_order', as: 'reject_job_order'
get '/job_orders/start_job_order/:id' => 'job_orders#start_job_order', as: 'start_job_order'
get '/job_orders/done_job_order/:id' => 'job_orders#done_job_order', as: 'done_job_order'
get '/job_orders/cancel_job_order/:id' => 'job_orders#cancel_job_order', as: 'cancel_job_order'
resources :job_orders do
resources :historys
end
resources :users
resources :offices
resources :user_sessions, only: [:create, :destroy]
resources :notifications do
collection do
post :mark_as_read
end
end
# resources :historys
end
答案 0 :(得分:0)
您想要特定工作订单的历史记录吗?
做
class JobOrder < ApplicationRecord
has_many :histories
end
然后您可以通过获取所有相关历史记录
@job_order.histories
在您看来,您可以做类似...的事情
<% @job_order.histories.each do |history| %>
<%= history.name %>
<%= history.comment %>
<br/>
<% end %>
偶然地,对“历史”的复数使用正确的拼写...您的控制器应为HistoriesController
Rails比您想象的要聪明。
答案 1 :(得分:0)
我还注意到您在params
和[:id]
之间留有一定距离。它应该是这样的:params[:id]
。