ruby on rails undefined method form_for

时间:2017-05-12 10:50:12

标签: ruby-on-rails ruby

我试图在我的应用程序中使用PaperClip。

我有3个型号:菜单,用户和酒吧。我希望用户能够为pub添加菜单,这是一个pdf文件。因此,当我上传pdf文件时,我希望在菜单模型中有一个带有pub id的列。

menu.rb

    has_attached_file :document
    validates_attachment :document, :content_type => {:content_type => %w(application/pdf)}

new.html.erb

    <div class="page-header"><h1>Upload Menu</h1></div>

    <%= form_for ([@pub, @menu]), html: { multipart: true } do |f| %>
    <%= render 'shared/error_messages', object: f.object %>

    <div class="form-group">
    <%= f.label :title %>
     <%= text_field :title, class: 'form-control' %>
    <%= f.label :document %>
    <%= f.file_field :document, class: 'form-control' %>
    <%= f.submit 'Upload Menu', class: 'btn btn-primary' %>
    </div>
    <% end %>

的routes.rb

     resources: pubs do
           resources :menus
     end

menus_controller.rb

     class MenusController < ApplicationController

     def index
        @menus = Menu.order('created_at')
     end
     def new
         @menu = Menu.new
     end
     def create
        @pub = Pub.find(params[:pub_id])
        input = menu_params.merge(pub: @pub)
        @menu = current_user.menus.build(input)
      if @menu.save
         flash[:success] = "Successfully added new menu!"
         redirect_to root_path
      else
         flash[:alert] = "Error adding new menu!"
         render :new
      end
      end


    private

   def menu_params
       params.require(:menu).permit(:title, :document)
   end

   end

新页面上传文件的按钮

        <%= link_to "Upload menu", new_pub_menu_path(@pub), class: 'btn btn-primary'  %>

所以当我点击按钮时,我会看到new_pub_menu_path(@pub)生成的链接,但是我有一个错误..

错误

ActionView::Template::Error (undefined method `menus_path' for #<#<Class:0x007f652ce54550>:0x007f652c90c9a8>
Did you mean?  user_path):
1: 
2: <%= form_for ([@pub, @menu]), html: { multipart: true } do |f| %>
3:   <%= render 'shared/error_messages', object: f.object %>
4: 
5:   <div class="form-group">

我该怎么办?我尝试使用嵌套路由,以便在url中有id pub,但是当new.html.file被渲染时,它会给我这个错误。我不知道menu_path是什么方法。 谢谢!

0 个答案:

没有答案