Ruby on Rails使用has_many:through relationship创建记录时遇到麻烦

时间:2013-10-15 16:44:52

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

我正在使用rails 3.2.14并且第一次使用has_many:through时遇到问题。我正在使用has_many:通过使用Design_Pictures表的Image和Design表之间的关系,该表将存储设计图像的订单排名。所有图像都与一个用户相关联。后来我希望能够灵活地在Images表中存储与特定设计无关的其他图像。

我可以成功地将样本数据添加到我的数据库中,并在我的节目设计页面中显示图像标题(存储在images表中)和排名(存储在design_pictures表中)。我无法弄清楚如何创建一个新的design_picture。一旦我能够正常工作,我将使用CarrierWave或Paperclip将图像添加到Image表中。

以下是我的模特:

应用/模型/ image.rb:

class Image < ActiveRecord::Base
  attr_accessible :title
  belongs_to :user
  has_many :design_pictures, dependent: :destroy 
  has_many :designs, :through => :design_pictures

  validates :user_id, presence: true
  validates :title, length: { maximum: 80 }
end

应用/模型/ design.rb:

class Design < ActiveRecord::Base
  attr_accessible :title
  has_many :design_pictures, dependent: :destroy 
  has_many :images, :through => :design_pictures

  validates :title, presence: true, length: { maximum: 60 }
end

应用/模型/ design_picture.rb:

class DesignPicture < ActiveRecord::Base
  attr_accessible :ranking
  belongs_to :image
  belongs_to :design

  validates :image_id, presence: true
  validates :design_id, presence: true   

  default_scope order: 'design_pictures.ranking ASC'
end

应用/模型/ user.rb:

class User < ActiveRecord::Base
  attr_accessible :name
  has_many :designs, dependent: :destroy 
  has_many :images, dependent: :destroy

  validates :name, presence: true, length: { maximum: 50 }
end

查看:

应用/视图/设计/ show.html.erb:

            <div>
                <% if @design.design_pictures.any? %>
                    <h3>Images (<%= @design.design_pictures.count %>)</h3>
                    <ul>
                        <%= render @design_pictures %>
                    </ul>
                <% end %>
            </div>

            <div>
                <%= render 'shared/design_picture_form' %>
            </div>

应用/视图/ design_pictures / _design_picture.html.erb

<li>
    <%= design_picture.image.title %> - Ranking: <%= design_picture.ranking %>
</li>

应用/视图/共享/ _design_picture_form.html.erb:

<%= form_for(@design_picture) do |f| %>
    <%= render 'shared/error_messages', object: f.object %>

    <%= f.label :title, "Title" %>
    <%= f.text_field :title, 
                placeholder: "Name your image.",
                class: "" %>

    <%= f.label :ranking, "Ranking" %>
    <%= f.text_field :ranking, 
            placeholder: "Rank your design picture.",
            class: "" %>            

    <%= hidden_field_tag(:design_id, @design.id) %>

    <%= f.submit "Add Image", class: "btn btn-large btn-primary" %>
<% end %>

控制器:

应用/控制器/ designs_controller.rb

    def show
        @design = Design.find(params[:id])
        @design_pictures = @design.design_pictures.find(:all, :limit => 10)

        @image = current_user.images.build 
        @design_picture = @design.design_pictures.build
    end

应用/控制器/ design_pictures_controller.rb

    def create
        @current_design = Design.find(params[:design_id])
        @image = current_user.images.new(params[:title])
        @design_picture = @current_design.design_pictures.build(:image_id => @image.id, params[:ranking])

        if @design_picture.save
            flash[:success] = "Micropost created!"
            redirect_to @current_design
        else
            redirect_to designs_url
        end
    end

访问design_picture表单partial时出错:

undefined method `title' for #<DesignPicture image_id: nil, design_id: 1427, ranking: nil>

如果我从表单中删除标题,我可以提交表单但不会创建任何内容。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

您的DesignPicture没有title属性。当你这样做时:

@design.design_pictures.build

它构建一个新的DesignPicture实例。以下不起作用:

<%= form_for(@design_picture) do |f| %>
  # ...

  <%= f.label :title, "Title" %>

  # ...

您可能需要查看Nested Form

答案 1 :(得分:0)

我不确定这是否是解决此问题的最佳方式,但这是我能够让我的表单提交并创建带有它的title属性的Image条目和带有它的rank属性的Design_Picture条目。我最终不需要使用accepts_nested_attributes_for。

应用/视图/共享/ _design_picture_form.html.erb

<%= form_for(@design_picture) do |f| %>
<%= render 'shared/error_messages', object: f.object %>

<%= label_tag(:title, "Title") %>
<%= text_field_tag(:title) %>   

<%= f.label :ranking, "Ranking" %>
<%= f.text_field :ranking, 
        placeholder: "Rank your design picture.",
        class: "" %>            

<%= hidden_field_tag(:design_id, @design.id) %>

<%= f.submit "Add Image", class: "btn btn-large btn-primary" %>

应用/控制器/ designs_controller.rb

def show
    @design = Design.find(params[:id])
    @design_pictures = @design.design_pictures.find(:all, :limit => 10)

    @design_picture = @design.design_pictures.build
end

应用/控制器/ design_pictures_controller.rb

def create
    @image = current_user.images.create(:title => params[:title])
    @current_design = Design.find(params[:design_id])       
    @design_picture = @current_design.design_pictures.build(params[:design_picture])
    @design_picture.image_id = @image.id

    if @design_picture.save
        flash[:success] = "Design Picture created!"
        redirect_to @current_design
    else
        redirect_to designs_url
    end
end