简体   繁体   中英

Ruby on Rails trouble creating record with has_many :through relationship

I'm using rails 3.2.14 and having trouble using has_many :through for the first time. I'm using a has_many :through relationship between Image and Design tables using a Design_Pictures table that will store the order ranking for design images. All Images are associated with one User. Later I want the flexibility to store other images in the Images table that are not associated with a particular design.

I can successfully add sample data to my database and show the image title (which is stored in the images table) and ranking (which is stored in the design_pictures table) in my show design pages. What I can't figure out is how to create a new design_picture. Once I can get this working I'm going to use CarrierWave or Paperclip to add images to the Image table.

Here are my models:

app/models/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

app/models/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

app/models/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

app/models/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

Views:

app/views/designs/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>

app/views/design_pictures/_design_picture.html.erb

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

app/views/shared/_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 %>

Controllers:

app/controllers/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

app/controllers/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

Error when I visit the design_picture form partial:

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

If I remove the title from the form I can submit form but nothing is created.

Any help would be greatly appreciated.

Your DesignPicture does not have a title attribute. When you do:

@design.design_pictures.build

It build a new DesignPicture instance. And the following do not work:

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

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

  # ...

You might want to take a look at Nested Form

I'm not sure if this is the best way to go about this but this is how I was able to get my form to submit and create both an Image entry with it's title attribute and the Design_Picture entry with it's rank attribute. I ended up not needing to use accepts_nested_attributes_for.

app/views/shared/_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" %>

app/controllers/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

app/controllers/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM