Rails4新模板.erb创建没有发生

时间:2013-12-18 10:16:27

标签: ruby-on-rails ruby-on-rails-4 mongoid

我已经生成了脚手架并创建了一个名为“约会”的视图 我想在约会文件夹中添加名为inbox_mail.html.erb的模板.erb文件。

我做了这样的设定。

route.rb
get '/appointments/inbox_mail'

在约会控制器中

class AppointmentsController < ApplicationController
  def inbox_mail
  end
end 

现在运行链接3000 / appointmentments / inbox_mail

但是会出现错误,

Mongoid::Errors::DocumentNotFound in AppointmentsController#show
Problem: Document(s) not found for class Appointment with id(s) delete_appointment. Summary: When calling Appointment.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): delete_appointment ... (1 total) and the following ids were not found: delete_appointment. Resolution: Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples.

帮助我使用Rails4 ...... !!!!

可能是这是

的b'z
def set_appointment
@appointment = Appointment.find(params[:id])
end 

2 个答案:

答案 0 :(得分:1)

是的,这是因为set_appointment方法。我猜您应该在路线中添加:id段,例如

match '/appointments/delete_appointment/:id', to: 'appointments#delete_appointment', via: :get

这应该有效。

答案 1 :(得分:1)

删除某些内容不应该通过GET完成,您应该使用DELETE方法。因此,当您使用“link_to”创建链接时,您应该执行以下操作:

link_to 'Delete appointment', delete_appointment_path(@appointment.id), method: :delete

你需要一条路线:

delete '/appointments/delete_appointment/:id', to: 'appointments#delete_appointment'

然后rails会处理这个并使用约会的id执行DELETE请求,然后在你的控制器上你可以使用@appointment = Appointment.find(params [:id])

您可能需要某种验证来呈现“未找到”模板:

def delete_appointment
  unless @appointment = Appointment.find(params[:id])
    redirect_to appointment_not_found_path #something_like_that
  end
end
编辑:看起来有些before_filter也搞乱了,你说的是“delete_appointment”,错误说调用的动作是“show”而你复制了action / before_filter“set_appointment”的代码,先检查一下

编辑2:你说你没有做任何删除,然后使用get,重要的部分是:url上的:如果你需要在网址上找到你需要的ID的约会。如果您不需要ID,请检查您之前的过滤器,我猜您有类似

的内容
before_filter :set_appointment

您可能想要在delete_appointment

上跳过该过滤器
before_filter :set_appointment, except: :delete_appointment