belongs_to 关系的嵌套属性

发布于 2024-12-28 19:20:09 字数 2307 浏览 1 评论 0原文

我有这个引脚模型:

class Pin < ActiveRecord::Base
  belongs_to :user
  belongs_to :image
  accepts_nested_attributes_for :image
  attr_accessible :image_attributes
end

我有这个图像模型:

class Image < ActiveRecord::Base
  has_many :pins
   validates_attachment_presence :attachment
   validates_attachment_size :attachment, :less_than => 5.megabytes
   validates_attachment_content_type :attachment, :content_type => ['image/jpeg', 'image/png']
   accepts_nested_attributes_for :pins
   attr_accessible :pins_attributes
end

为了创建一个新引脚,我想使用图像的嵌套属性,但它不起作用:我在控制器和视图文件中使用的代码是:

def new
    @pin = Pin.new
    @pin.build_image if @pin.build_image.nil?
end

 def create
  @pin = Pin.new(params[:pin])
   @pin.user_id  = session[:user_id]
  respond_to do |format|
    if @pin.save
      format.html { redirect_to(@pin, :notice => 'Pin was successfully created.') }
      format.xml  { render :xml => @pin, :status => :created, :location => @pin }
    else
      @boards = User.find(session[:user_id]).boards
      format.html { render :action => "new" }
      format.xml  { render :xml => @pin.errors, :status => :unprocessable_entity }
    end
  end
end

并且在视图文件中:

   <%= form_for @pin,:html=>{ :multipart => true} do |f| %>
    <% if @pin.errors.any? %>
    <div id="error_explanation">
        <h2><%= pluralize(@pin.errors.count, "error") %> prohibited this pin from being saved:</h2>

        <ul>
            <% @pin.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
            <% end %>
        </ul>
    </div>
    <% end %>



    <div class="field">
        <%= f.label :title %>
        <%= f.text_area :title %>
    </div>
    <%= f.fields_for :image do |f_i| %>
    <div class="field">
        <%= f_i.label :attachment %>
        <%= f_i.file_field :attachment %>
    </div>
    <% end %>
    <div class="actions">
        <%= f.submit %>
    </div>
    <% end %>

始终形式渲染错误:

     Image attachment file name must be set.

I have this Pin Model:

class Pin < ActiveRecord::Base
  belongs_to :user
  belongs_to :image
  accepts_nested_attributes_for :image
  attr_accessible :image_attributes
end

I have this Image Model:

class Image < ActiveRecord::Base
  has_many :pins
   validates_attachment_presence :attachment
   validates_attachment_size :attachment, :less_than => 5.megabytes
   validates_attachment_content_type :attachment, :content_type => ['image/jpeg', 'image/png']
   accepts_nested_attributes_for :pins
   attr_accessible :pins_attributes
end

For creating a New Pin I want to use nested attributes for image but its not working: Code I am using in controller and view file are:

def new
    @pin = Pin.new
    @pin.build_image if @pin.build_image.nil?
end

 def create
  @pin = Pin.new(params[:pin])
   @pin.user_id  = session[:user_id]
  respond_to do |format|
    if @pin.save
      format.html { redirect_to(@pin, :notice => 'Pin was successfully created.') }
      format.xml  { render :xml => @pin, :status => :created, :location => @pin }
    else
      @boards = User.find(session[:user_id]).boards
      format.html { render :action => "new" }
      format.xml  { render :xml => @pin.errors, :status => :unprocessable_entity }
    end
  end
end

And In view file:

   <%= form_for @pin,:html=>{ :multipart => true} do |f| %>
    <% if @pin.errors.any? %>
    <div id="error_explanation">
        <h2><%= pluralize(@pin.errors.count, "error") %> prohibited this pin from being saved:</h2>

        <ul>
            <% @pin.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
            <% end %>
        </ul>
    </div>
    <% end %>



    <div class="field">
        <%= f.label :title %>
        <%= f.text_area :title %>
    </div>
    <%= f.fields_for :image do |f_i| %>
    <div class="field">
        <%= f_i.label :attachment %>
        <%= f_i.file_field :attachment %>
    </div>
    <% end %>
    <div class="actions">
        <%= f.submit %>
    </div>
    <% end %>

All the time form is rendering an error:

     Image attachment file name must be set.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

a√萤火虫的光℡ 2025-01-04 19:20:09

我想说问题出在图像模型中的 attr_accessible :pins_attributes 和 Pin 模型中的另一个 attr_accessible 。通常,除了 id 之外的所有属性都是可访问的(意味着可以批量分配),但是如果您将属性声明为可访问,则必须列出要批量分配的所有属性。因此,尝试列出两个模型中的属性,或者仅设置 attr_accessible :all

I would say the problem is with attr_accessible :pins_attributes in Image model and another attr_accessible in Pin model. Usually all the attributes except the id are accessible (means can be mass-assigned), but if you declare an attribute as accessible - you have to list all the attributes you want to be mass-assigned. So, try to list the attributes in both models, or just set attr_accessible :all

北凤男飞 2025-01-04 19:20:09

在图像模型中我需要指定

  has_attached_file :attachment, :styles => { :small => "150x150>" }

我的坏:(

In Image model I need to specify

  has_attached_file :attachment, :styles => { :small => "150x150>" }

my bad :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文