首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails:用于#<#<Class:>>的未定义方法‘’

Rails:用于#<#<Class:>>的未定义方法‘’
EN

Stack Overflow用户
提问于 2013-07-31 19:52:09
回答 3查看 13.6K关注 0票数 2

我对rails还比较陌生,几天来我一直在努力解决这个问题。如果你能看到我做错了什么,我会非常感激的。

当我在web浏览器中查看页面时,会收到以下消息:

显示第8行所显示的C:/Users/Matt/Documents/GitHub/Outputer/app/views/studies/index.html.erb: 用于`studies_path的未定义方法#<#:0x6b03808>‘ 8:<%= form_for @new_study do \f\x %>

studies_controller:

代码语言:javascript
复制
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

代码语言:javascript
复制
....
<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>
....

学习模型

代码语言:javascript
复制
....
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
....

rake路由:

代码语言:javascript
复制
....
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

代码语言:javascript
复制
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

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

期待中的感谢

EN

回答 3

Stack Overflow用户

发布于 2013-07-31 19:58:22

当您将form_for与模型实例一起使用时,它默认为该控制器的POST操作,即您的studies_path。这通常被映射到控制器中的create

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

这里是学习rails路由基础知识的一个很好的指南。

票数 1
EN

Stack Overflow用户

发布于 2014-02-28 18:47:49

虽然丢失路由是导致该错误(没有多大帮助)的最常见原因,但如果一个has_ for /to_to关系的一方或双方丢失或定义不正确,也会引发该错误。另一个要查看的地方是有关模型中不存在的属性的表单字段。

票数 1
EN

Stack Overflow用户

发布于 2014-02-28 19:02:10

<%= form_for @new_study %>等同于<%= form_for @new_study, url: studies_url %>。由于您的路由定义不同,您需要将要将表单提交给url参数的url传递给form_for参数(在Rails API文档中查找form_for以查看它需要哪些其他选项)。

三层深巢很难维持,所以我建议如下:

代码语言:javascript
复制
resources :users

resources :lines do
  resources :machines
end

resources :machines do
  resources :studies 
end

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

就你而言:

代码语言:javascript
复制
# 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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17979645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档