2013-07-31 53 views
2

未定义的方法`”我是比较新的轨道,我一直是这样挣扎了几天。如果你能看到我出错的地方,我会非常感激。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 

如果我删除表单的页面正常工作这将表明它的形式。我已经在控制台中测试了控制器命令,它们都显得很好 - 我可以创建一个新的研究对象。

感谢预期

回答

1

当您使用form_for一个模型实例,则默认为POST行动为控制器,它会成为你studies_path。这通常映射到控制器中的create

从外观上来看,你需要在routes.rb添加路由来处理POST请求(请参阅参考资料)。您还需要在你的学习控制器create方法。

Here是学习的轨道路由的基本知识有很好的指导。

+0

谢谢。所以我有一条创建路线 - 我编辑了帖子并添加了代码。目前我无法在查看index.html时加载表单 - 我还没有完成提交,所以我不认为我正在创建控制器。 – mpnh

+0

即使您有创建方法,但您没有实际的路线。没有什么可以告诉它使用这种方法。表单试图加载'studies_path'so或者将路由添加到routes.rb,或者为要使用的表单指定一个不同的路径。 –

+0

我已经添加了routes.rb的内容。我可能在这里遇到了一些问题,但我的理解是,'资源:研究'应该创建所有路线,包括创建 – mpnh

1

虽然缺少路由是导致该错误的最常见原因,但是如果has_many/belongs_to关系的一方或双方缺失或未正确定义,也会引发此错误。另一个要查看的地方是相关模型中不存在的属性的表单字段。

0

<%= form_for @new_study %>相当于<%= form_for @new_study, url: studies_url %>。当你的路线不同的定义,你需要通过你想要的形式提交给url参数(发现在Rails的API文档form_for来看看需要什么其他选项)的URL。

三级深度嵌套是一种丑恶来维持,所以我建议如下:

resources :users 

resources :lines do 
    resources :machines 
end 

resources :machines do 
    resources :studies 
end 

这些shallow路线是好得多维护。关于嵌套资源调用,还有一个shallow: true选项,请参阅文档。

你的情况:

# With the current setup 
<%= form_for @new_study, url: line_machine_studies_path(@line, @machine) 
# Same, my preference 
<%= form_for [@line, @machine, @new_study] %> 

# If you make your routes shallow, 
# @line is not nescessary, as @machine holds all information about associations 

<%= form_for @new_study, url: machine_studies_path(@machine) %> 
# Same, my preference, what I would do 
<%= form_for [@machine, @new_study] %> 

一般建议:

  • @study优于@new_study。如果需要,@study.new_record?会告诉你对象是否是新记录。
  • 有资源的路线没有has_many :...选项,据我所知
  • 谷歌rails shallow routes获取更多信息。保持嵌套到两个级别。想想只有在创建对象时确实需要哪些信息,并尽可能保持URL和url助手的纤细。
+0

这些例子可能需要在他们的'form_for'中使用'do'。 – IAmNaN

相关问题