【发布时间】:2019-08-29 12:49:30
【问题描述】:
寻求帮助,我认为我在做一些愚蠢的事情,但我不知道是什么。我创建了一个link_to 来远程触发ruby 中的控制器操作,这样我就可以将问题标记为“已解决”(发送问题id 和patient_id)。
当我在PatientsController 的show 视图中而不是NotesController 的new 视图中时,此方法有效。我正在为人道主义部门设计一个精简的患者记录系统。
我不明白为什么它在一个视图中起作用,而在另一个视图中不起作用?!
很高兴以不同的方式来做这件事,如果这不是适当的方式来做到这一点。如果有人需要更多信息,请告诉我。
提前致谢
奥利
错误
ActionController::UrlGenerationError in Notes#new
没有路由匹配 {:action=>"resolve", :controller=>"issues", :issue_id=>"1", :patient_id=>2}
路线
resolve_issue GET /issues/:id/resolve(.:format)
问题#解决
查看部分代码
这是在某些视图上引发错误的代码。
这嵌入在.each 循环中,我知道它在某些视图上输出,如果我删除以下行,那么它会在所有视图上输出。
<%= link_to "<span class='caption'>Resolve</span><i class='material-icons left'>check</i>".html_safe,
{ :controller => "issues", :action => "resolve", :remote => true, :patient_id => issue.patient.id, :issue_id => issue.id, :message => "URL passed message" },
:class => "btn waves-effect waves-light icon-to-text-btn red" %>
患者控制器代码 - 工作
def show
session[:return_to] ||= request.referer
@patient = Patient.find(params[:id])
session[:patient_id] = params[:id]
@obs = @patient.observations.last
@pagy, @notes = pagy( Note.where(:patient_id => @patient.id).reverse_order, items: 10 )
render layout: "no-card"
end
注意控制器代码 - 错误
def new
@note = Note.new
@note.patient_id = params[:patient_id]
@note.user_id = params[current_user.id]
@note.note_time = Time.now
@patient = Patient.find(params[:patient_id])
session[:patient_id] = params[:patient_id]
end
问题控制器代码
这按预期工作。
def resolve
@issue = Issue.find(params[:issue_id])
@patient = Patient.find(params[:patient_id])
if @issue.update_attributes(:resolved => Time.now, :resolved_by => current_user.id, :resolved_note => "Hard coded test message")
flash[:success] = "Resolved " + @issue.title
else
flash[:error] = "Failed to resolve " + @issue.title
end
respond_to do |format|
format.js { render :refresh } # this will look for a file names create.js.erb in views/links directory
end
end
路由文件
Rails.application.routes.draw do
resources :allergies
resources :prescriptions
resources :issues
get 'issues/:id/resolve' => 'issues#resolve', as: :resolve_issue
get 'notes/:id/sign' => 'notes#sign', as: :sign_note
get 'notes/:id/hide' => 'notes#hide', as: :hide_note
get 'patients/:id/show' => 'patients#show', as: :show_patient
resources :notes
resources :observations
resources :patients
end
【问题讨论】:
-
可能值得发布您的
routes.rb文件的内容 -
我也没有看到你在哪里使用
:resolve方法?请同时发布您的issue controller,因为到目前为止我从您的代码中了解到,您尝试在不存在的issue控制器中运行resolve方法
标签: ruby-on-rails ruby