未定义的方法`”我是比较新的轨道,我一直是这样挣扎了几天。如果你能看到我出错的地方,我会非常感激。Ruby on Rails的:对#<#<Class:>>
当我在Web浏览器中,我得到了以下信息查看页面:
显示C:/Users/Matt/Documents/GitHub/Outputer/app/views/studies/index.html.erb其中线#8提出:
未定义的方法`studies_path”为#<#:0x6b03808>
8:<%=的form_for @new_study做| F | %>
studies_controller:
def index
@line = current_user.lines.find_by_id(params[:line_id])
@machine = @line.machines.find_by_id(params[:machine_id])
@studies = @machine.studies.paginate(page: params[:page], :per_page => 10)
@new_study = @machine.studies.build
end
def create
@study = current_user.lines.machines.study.build(params[:study])
if @study.save
flash[:success] = "Study created"
else
flash[:error] = "Error : Invalid study description"
end
redirect_to :back
end
的index.html
....
<section>
<%= form_for @new_study do |f| %>
<div class="field">
<%= f.text_field :description, placeholder: "New study description..." %>
</div>
<%= f.submit "Create", class: "btn" %>
<% end %>
</section>
....
研究模型
....
class Study < ActiveRecord::Base
belongs_to :machine
belongs_to :line
attr_accessible :avg_speed, :avg_uptime, :avg_yield, :description, :duration, :is_active, :start_time, :stop_time, :line_id
validates ....
has_many :events, dependent: :destroy
....
end
....
条
耙路线:
....
save_line_machine_study PUT /lines/:line_id/machines/:machine_id/studies/:id/save(.:format) studies#save {:has_many=>:machines}
line_machine_studies GET /lines/:line_id/machines/:machine_id/studies(.:format) studies#index {:has_many=>:machines}
POST /lines/:line_id/machines/:machine_id/studies(.:format) studies#create {:has_many=>:machines}
new_line_machine_study GET /lines/:line_id/machines/:machine_id/studies/new(.:format) studies#new {:has_many=>:machines}
edit_line_machine_study GET /lines/:line_id/machines/:machine_id/studies/:id/edit(.:format) studies#edit {:has_many=>:machines}
line_machine_study GET /lines/:line_id/machines/:machine_id/studies/:id(.:format) studies#show {:has_many=>:machines}
PUT /lines/:line_id/machines/:machine_id/studies/:id(.:format) studies#update {:has_many=>:machines}
DELETE /lines/:line_id/machines/:machine_id/studies/:id(.:format) studies#destroy {:has_many=>:machines}
....
的routes.rb
resources :users
resources :lines, :has_many => :machines, only: [:index, :edit, :destroy, :show, :create] do
resources :machines, only: [:new, :create, :edit, :update] do
resources :studies
end
end
如果我删除表单的页面正常工作这将表明它的形式。我已经在控制台中测试了控制器命令,它们都显得很好 - 我可以创建一个新的研究对象。
感谢预期
谢谢。所以我有一条创建路线 - 我编辑了帖子并添加了代码。目前我无法在查看index.html时加载表单 - 我还没有完成提交,所以我不认为我正在创建控制器。 – mpnh
即使您有创建方法,但您没有实际的路线。没有什么可以告诉它使用这种方法。表单试图加载'studies_path'so或者将路由添加到routes.rb,或者为要使用的表单指定一个不同的路径。 –
我已经添加了routes.rb的内容。我可能在这里遇到了一些问题,但我的理解是,'资源:研究'应该创建所有路线,包括创建 – mpnh