如何在轨道上的红宝石中嵌套多个模型4

时间:2014-02-07 16:42:47

标签: ruby-on-rails ruby

我试图以interest_point形式嵌套模型,目前已经有一个嵌套模型在我尝试添加其他模型来存储艺术家信息之前工作正常。我试图将它们都嵌套,并且不明白它为什么不起作用。这是我的表单代码:

<%= form_for @interest_point, :url => interest_points_path, :html => {:multipart => true} do |f| %>

<div id="errors"></div><% if @interest_point.errors.any? %>
  <h3><%= pluralize(@interest_point.errors.count, 'error')%> prevented this form from being saved.</h3>
  <ul>
    <% @interest_point.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
  </ul>
  <% end %>
</div>

<p>Name:  <%= f.text_field(:name) %></p>
<p>Type:  <%= f.select(:interest_type, options_for_select([['City', 'City']]))%></p>
<!-- TODO: add if/then logic to only show artist first_name & last_name fields -->
<% f.fields_for(:artists) do |artist_form| %>
  <p>Artist First Name: <%= artist_form.text_field :first_name %></p>
  <p>Artist Last Name: <%= artist_form.text_field :last_name %></p>
<% end %>
<!-- END ARTIST INFO -->
<p>Latitude: <%= f.text_field(:latitude) %></p>
<p>Longitude: <%= f.text_field(:longitude) %></p>

<!-- Start Upload Photo section -->
<%= f.fields_for(:images) do |builder| %>
  <% if builder.object.new_record? %>
    <p><%= builder.file_field(:photo) %></p>
  <% else %>
    <% @interest_point.images.each do |image| %>
      <%= image_tag(image.photo.url) if image.photo? %>
    <% end %>
  <% end %>
<% end %>
<!-- END Upload Photo section -->

<p>Summary:<br /><%= f.text_area(:summary,  size: "50x10") %></p>
<p><%= f.submit("Save Interest Point") %></p>
<% end %>

这是models / artist.rb

class Artist < ActiveRecord::Base
  belongs_to :interest_point

  validates :first_name, :last_name, presence: true
end

模型/ interest_point.rb

# An +Interest Point+ is a specific instance of any location that may have special     meaning to one or more users. Once an +Interest Point+ is added to the application by a     user, others are able to locate that instance on a map, view specific details, and leave     individual Comments.  User are also able to share an +Interest Point+ with a friend using     our share feature (future). 
# @author   Ashley Childress
# @version 1.23.2014
class InterestPoint < ActiveRecord::Base    
  belongs_to :creator, :class_name => 'User', :foreign_key => 'contributor_id'
  belongs_to :approver, :class_name => 'User', :foreign_key => 'approver_id'

  has_many :artists
  accepts_nested_attributes_for :artists

  # One or more +Comment+ object that may be associated with this +Interest Point+.
  # @return [Array<Comment>]
  has_many :comments, as: :commentable, dependent: :destroy

  # A set of images that have been uploaded by the user that show this +Interest     Point+.
  # @see #Image
  # @note Only .jpg, .jpeg, .gif, or .png files are acceptable.
  has_many :images, :dependent => :destroy
  accepts_nested_attributes_for :images, :reject_if => lambda{ |t| t['photo'].nil? },     :allow_destroy => true

  validates :name, :latitude, :longitude, presence: true

  before_save :normalize_coordinates

  protected
  # Ensure the given +latitude+ and +longitude+ coordinates are rounded to four     decimal places.
  # @pre The +latitude+ and +longitude+ must be defined as valid decimal values before     this method is called
  # @post The +latitude+ and +longitude+ values are replaced with their equivalent     values rounded to four decimal places
  def normalize_coordinates
    self.latitude = latitude.round(4)
    self.longitude = longitude.round(4)
  end   
end

和controllers / interest_points_controller.rb:

# This +InterestPointsController+ should be used to control new +InterestPoint+     instances that are added to this application.
# @author   Ashley Childress
# @version 1.25.2014
class InterestPointsController < ApplicationController

  # Create a new, empty +Interest Point+ and build any associated +Images+.
  def new
    @interest_point = InterestPoint.new
    @interest_point.artists.build
    @interest_point.images.build
  end

  # Create a new +Interest Point+ with attributes equal to the given parameters.
  # @param [String] name the title used to identify the +Interest Point+
  # @param [Float] latitude the location given as a northern coordinate degree
  # @param [Float] longitude the location given as a western coordinate degree
  # @param [String] interest_type the category the +Interest Point+ should be defined under
  # @param [String] summary a description of this +Interest Point+ that will be displayed to users on the details page
  # @param [Image] photo the .jpg, .jpeg, .gif, or .png file that will be displayed for this +Interest Point+
  def create
    @interest_point = InterestPoint.new(interest_point_params)
    if @interest_point.save
      flash[:notice] = "#{@interest_point.name} was created successfully!"
      redirect_to interest_point_path(@interest_point)
    else
      render 'new'
    end
  end

0 个答案:

没有答案