【问题标题】:rails form_helper could not find path for model in edit pagerails form_helper 在编辑页面中找不到模型的路径
【发布时间】:2012-04-27 07:26:18
【问题描述】:

我有 Picture 模型,它包含 2 个字段 照片和描述(照片是来自 papperclip gem 的字段)

我有 PicturesController 包含新的创建编辑更新销毁和显示

我已经使用resource :picturesroutes.rb 中声明,并且有我的路线。

     pictures POST   /pictures(.:format)      pictures#create
 new_pictures GET    /pictures/new(.:format)  pictures#new
edit_pictures GET    /pictures/edit(.:format) pictures#edit
              GET    /pictures(.:format)      pictures#show
              PUT    /pictures(.:format)      pictures#update
              DELETE /pictures(.:format)      pictures#destroy

有我的查看代码

  # pictures/new.html.erb & pictures/edit.html.erb 
  <%= form_for @picture, :html => {:multipart => true} do |f| %>

      <div><%= f.label :photo %><br/>
        <%= f.file_field :photo %></div><br/>

      <div><%= f.label :description %><br/>
        <%= f.text_area :description %></div><br/>

      <%= f.submit :upload %>
  <% end %>

  # 

我在新建和编辑中使用相同的代码,只是更改提交按钮标签。

new.html.erb 工作正常(创建图片并将其保存到数据库中)。

但错误出现在edit.html.erb

 undefined method `picture_path' for #<#<Class:0x007f830e93ad30>:0x007f830e92d5b8>

我已经检查了@picture。为什么 rails 找不到 Picture 类的更新路径?

我遵循这个link 的form_for 指南。

有什么想法吗?谢谢。

【问题讨论】:

  • 请添加newedit 操作的代码。
  • @Thilo 问题出在我的路线上。谢谢你的答案。 :)

标签: ruby-on-rails paperclip ruby-on-rails-3.2


【解决方案1】:

您的复数形式有问题。使用资源 :pictures 你应该有类似的东西..

pictures     GET    /pictures(.:format)                    pictures#index
             POST   /pictures(.:format)                    pictures#create
new_picture  GET    /pictures/new(.:format)                pictures#new
edit_picture GET    /pictures/:id/edit(.:format)           pictures#edit
picture      GET    /pictures/:id(.:format)                pictures#show
             PUT    /pictures/:id(.:format)                pictures#update
             DELETE /pictures/:id(.:format)                pictures#destroy

注意图片元素上的单数......这将产生动态图片路径方法

更新:在你的路由文件中将'resource'更改为'resources',你应该没问题

【讨论】:

  • 是的,它现在可以工作了。一时间我觉得很傻。谢谢:D
  • 每个使用 Rails 的人都曾在某个时候被复数问题所困扰;-)
  • 哈哈哈。我同意。 (我第一次发现有表名并卡住了 2 天。):)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多