Ruby on rails 多态嵌套路由
这是第一次使用多态关联。我有一个属于用户模型和记分板模型的图片模型。图片模型的迁移如下所示Ruby on rails 多态嵌套路由,ruby-on-rails,Ruby On Rails,这是第一次使用多态关联。我有一个属于用户模型和记分板模型的图片模型。图片模型的迁移如下所示 class CreatePictures < ActiveRecord::Migration def change create_table :pictures do |t| t.string :picture t.references :imageable, index: true, polymorphic: true t.timestamps n
class CreatePictures < ActiveRecord::Migration
def change
create_table :pictures do |t|
t.string :picture
t.references :imageable, index: true, polymorphic: true
t.timestamps null: false
end
add_foreign_key :pictures, :imageables
end
add_index :pictures, :imageable_id
# add index on the pictues table on the imageable_id column
end
class Scoreboard < ActiveRecord::Base
has_one :picture, as: :imageable
end
class User < ActiveRecord::Base
has_one :picture, as: :imageable
end
class Picture < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
end
我有scoreboard\u图片控制器,使用以下新方法
class ScoreboardPictureController < ApplicationController
def new
@scoreboard = Scoreboard.find(params[:scoreboard_id])
@picture = @scoreboard.build_picture
end
我以前实现过嵌套路由,它没有给我带来任何问题。然而,我不断得到以下错误
undefined local variable or method `scoreboards_picture_path' for #<#<Class:0x007fa4c054f490>:0x00000007574700>
未定义的局部变量或方法“scoreboards\u picture\u path”#
我认为这与多态关联有关。我尝试在表单中提供URL。它仍然不起作用。我读了一些关于这个问题的其他帖子,并尝试使用对他们有用的东西,但仍然得到了错误。我确信我的新方法是正确的。我已经在这上面呆了一段时间了。任何帮助都将不胜感激。它可能需要记分牌\u图片\u路径
。您可以通过运行rake routes
并查看左栏来确认这一点。它应该是scoreboards\u scoreboard\u picture\u path
@PardeepDhingra我以前尝试过,但没有成功。我现在也试过了。不确定是什么原因problem@dwenzel我试过了,但没用。运行rake路径有帮助。这是新的记分牌、记分牌、图片、路径。谢谢你的帮助!
<%= form_for([@scoreboard, @picture], url: scoreboards_picture_path, html: {multipart: true}) do |f| %>
<%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png', id: "files" %>
<% end %>
undefined local variable or method `scoreboards_picture_path' for #<#<Class:0x007fa4c054f490>:0x00000007574700>