2012-11-26 3 views
1

http://www.emersonlackey.com/article/rails-paperclip-multiple-file-uploads 튜토리얼 스크린 캐스트를 따랐습니다. 내 모델에 여러 사진 업로드 표시가 필요합니다.보호 된 속성을 대량 지정할 수 없습니다. asset

주의 깊게 모든 단계를 조사했는데, 가장 일반적인 문제는 attr_accessible에 assets_attributes를 추가하는 것을 잊었습니다. 또 다른 문제는 자산 모델에 ID를 추가하는 것을 잊어 버릴 수도 있습니다. 그러나, 나는 그것이 왜 일어나는지 이해하는 데 여전히 어려움을 겪고있다.

Can't mass-assign protected attributes: asset in app/controllers/posts_controller.rb:24:in `update' 

게시 용 게시물 모델에 대한 모든 속성 목록을 이미 추가했습니다. 마찬가지로 : 여기

class Post < ActiveRecord::Base 
    attr_accessible :name, :content, :assets_attributes 
    validates :user_id, presence: true 
    belongs_to :user 
    has_many :assets 
    accepts_nested_attributes_for :assets, :allow_destroy => true 
    default_scope order: 'posts.created_at DESC' 
end 

가 post_controller.rb 파일입니다

class Asset < ActiveRecord::Base 
     belongs_to :post 
     has_attached_file :asset, 
       :style => { :large => "640x480", :medium => "300x300", :thumb => "100x100"} , 
       :path => ":rails_root/public/system/posts/images/:id/:style/:filename", 
       :url => "/system/posts/images/:id/:style/:filename" 
    end 

:

다음
 <%= simple_form_for(@post, :html => {:multipart => true}) do |f| %> 
      <%= f.label :name %> 
      <%= f.text_field :name %> 
      <%= f.label :content %> 
      <%= f.text_field :content %> 
      <%= f.simple_fields_for :assets, :html => { :multipart => true } do |asset_fields| %> 
       <% if asset_fields.object.new_record? %> 
        <P><%= asset_fields.file_field :asset %> </P> 
       <% end %> 
      <% end %> 
      <%= f.simple_fields_for :assets, :html => { :multipart => true } do |asset_fields| %> 
       <% unless asset_fields.object.new_record? %> 
        <P><%= link_to image_tag(asset_fields.object.asset.url(:thumb)), asset_fields.objects.asset.url(:original) %> 
        <%= asset_fields.check_box :_destroy %></P> 
       <% end %> 
      <% end %> 

가 asset.rb입니다 : 여기

def edit 
    @post = Post.find(params[:id]) 
    5.times { @post.assets.build } 
end 
def update 
    @post = Post.find(params[:id]) 
    if @post.update_attributes(params[:post])  
      redirect_to @post, :notice => "Post has been updated." 
end 
def create 
    post = current_user.posts.build(params[:post]) 
    if post.save 
     flash[:success] = "post created success!" 
     redirect_to @post 
    else 
     @feed_items = [] 
     flash[:failure] = "post created fail!" 
     redirect_to root_path 
    end 
end 
def new 
    @post = current_user.posts.new #if signed_in? 
    5.times { @post.assets.build } 
end 

하는 템플릿 파일입니다 누군가 나에게 힌트를 줄 수 있습니까? 고마워요!

+2

'Post' 모델에'post_id' 속성이있는 이유는 무엇입니까? 아니면 그냥 오타입니까? – Adnan

+0

예, 오타입니다. 나는 그것을 제거했다. – Imhotep

+0

템플릿 오류 로그 파일에 "<% = asset_fields.file_field : asset %>"이 있기 때문에이 오류가 발생했습니다. "attr_accessible : name, : content, : assets_attributes asset"과 같이 attr_accessible에 자산을 추가해야합니까? – Imhotep

답변

2

Asset 모델에는 특히 asset 필드의 경우 attr_accessible이 있어야합니다.

+0

attr_accessible : asset을 자산 모델에 추가하면 작동합니다! 고마워요! 어제부터이 문제로 고생하고 있습니다. – Imhotep

관련 문제