我有一个没有索引页的调查模型。我只想要这个模型的一个编辑视图,但是看起来rails不允许我这样做。当我试图使用undefined method surveys_path
时,它会抱怨form_for(@survey)
。在不创建空索引路由/视图的情况下执行此操作是否可行。
这是我的测量控制员
class SurveysController < ApplicationController
def show
@survey = Survey.find(params[:id])
end
def edit
@survey = Survey.new
job = Job.find(params[:id])
@survey.job_id = job.id
authorized_user = job.user
unless !is_runner?(current_login) && current_login.id == authorized_user.id
redirect_to jobs_path
end
end
def update
@survey = Survey.new(params[:survey])
end
end
下面是在edit.html.erb中呈现的表单部分
<%= form_for(@survey) do |f| %>
<div class="field">
<%= f.label :speed %><br />
<%= f.text_field :speed %>
</div>
<div class="field">
<%= f.label :service %><br />
<%= f.text_field :service %>
</div>
<div class="field">
<%= f.label :suggestion %><br />
<%= f.text_area :suggestion %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
最佳答案
它不应该仅仅为了定义表单而询问surveys_path
,但是控制器代码中有一些奇怪的地方可能会让您感到悲伤。
在restful应用程序的surveys控制器中,您的编辑操作应该使用该params[:id]来查找现有的调查,为什么要查找作业?它应该和你的表演动作一样。
“测量编辑”方法中的测量对象是新的且未保存的,因此表单生成器将生成指向“创建”操作而不是“更新”操作的表单。表单生成器将仅为persisted?
的记录生成编辑表单。
你为这个模型定义了路线了吗?您的路由文件中应该包含以下内容:
resources :surveys, :except => [:index] # will create all rest routes for survey model except for an index route.