2

我对 Rails 比较陌生,而且我已经为此苦苦挣扎了几天。如果您能看到我哪里出错了,我将不胜感激。

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

显示 C:/Users/Matt/Documents/GitHub/Outputer/app/views/studies/index.html.erb 其中第 8 行提出:

#<#:0x6b03808> 的未定义方法“studies_path”

8: <%= form_for @new_study 做 |f| %>

研究控制器:

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

索引.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}
....

路线.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

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

感谢期待

4

3 回答 3

1

尽管缺少路由是导致该错误(不是很有帮助)的最常见原因,但如果 has_many/belongs_to 关系的一侧或两侧丢失或定义不正确,也会引发此错误。另一个需要查看的地方是相关模型中不存在的属性的表单字段。

于 2014-02-28T18:47:49.913 回答
1

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

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

是学习 Rails 布线基础知识的好指南。

于 2013-07-31T19:58:22.367 回答
0

<%= form_for @new_study %> is equivalent to <%= form_for @new_study, url: studies_url %>. As your routes are defined differently, you need to pass the url you'd like to submit the form to to the url parameter (find form_for in the Rails API docs to see what other options it takes).

Three level deep nesting is kind of ugly to maintain, so I'd suggest the following:

resources :users

resources :lines do
  resources :machines
end

resources :machines do
  resources :studies 
end

These shallow routes are much nicer to maintain. There's also a shallow: true option on nested resources calls, see the docs.

In your case:

# 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] %>

General suggestions:

  • @study is preferred over @new_study. @study.new_record? will tell you whether the object is a new record if you need.
  • There's no has_many :... option on resources routes as far as I'm aware
  • Google rails shallow routes for more info. Keep nesting to two levels. Think about only what information you really require when creating objects and keep the URLs and url helpers as slim as possible.
于 2014-02-28T19:02:10.557 回答