我有一个没有索引页的调查模型。我只想要这个模型的编辑 View ,但 Rails 似乎不允许我这样做。当我尝试使用 form_for(@survey)
时,它会提示 undefined method surveys_path
。无论如何,在不创建空索引路由/ View 的情况下是否可以这样做。
到目前为止,这是我的调查 Controller
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
,但是您的 Controller 代码中有一些奇怪的地方可能会让您感到悲伤。
- 在 RESTful 应用程序的调查 Controller 中,您的编辑操作应该使用该参数 [:id] 来查找现有调查,为什么要查找工作?它应该与您的表演 Action 相同。
- 调查编辑方法中的调查对象是新的且未保存的,因此表单生成器将生成一个指向创建操作而不是更新操作的表单。表单生成器将仅为
persisted?
的记录生成编辑表单
您是否为该模型定义了路线?你的路由文件中应该有如下内容:
resources :surveys, :except => [:index] # will create all rest routes for survey model except for an index route.
关于ruby-on-rails - rails : form_for active record object complaining about no index path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16972829/