ruby on rails - Unpermitted parameter in nested attributes using Paperclip -
i'm looking add multiple images using paperclip via nested attributes in rails 5.
i'm not sure i'm missing, getting following error referencing attributes:
unpermitted parameter: :image i'm referencing image attribute in news controller attributes, see below. image doesn't save database.
models
class news < applicationrecord has_many :images, dependent: :destroy accepts_nested_attributes_for :images, allow_destroy: true end class image < applicationrecord belongs_to :news has_attached_file :image, :styles => { :show => "600x600>" }, size: { less_than: 2.megabytes } validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/gif", "image/png"] end controller
class newscontroller < applicationcontroller def new @news = news.new @news.images.build end def create @news = news.new(news_params) respond_to |format| if @news.save format.html { redirect_to @news, notice: 'news created.' } format.json { render :show, status: :created, location: @news } else format.html { render :new } format.json { render json: @news.errors, status: :unprocessable_entity } end end end private def set_news @news = news.find(params[:id]) end def news_params params.require(:news).permit(:title, :description, :category, images_attributes: [:id, :image, :news_id, :_destroy]) end end form
<%= form.fields_for :image |img| %> <%= img.file_field :image, multiple: true %> <% end%> sent results
parameters: {"utf8"=>"✓", "authenticity_token"=>"kfpv2dze6j9uhi4pu1qm+zgdwxbfadjd2kjlhmlmk7losumgb0vwqwa6zpjre0b38jssuzhykrd4g6xayegmwa==", "news"=>{"title"=>"news title", "description"=>"random text goes in here.", "category"=>"", "image"=>{"image"=>#<actiondispatch::http::uploadedfile:0x007feed58a5ba8 @tempfile=#<tempfile:/var/folders/n1/dt5dwx0n7rx59_3bpvp64x400000gp/t/rackmultipart20170822-995-firof5.jpg>, @original_filename="03.jpg", @content_type="image/jpeg", @headers="content-disposition: form-data; name=\"news[image][image]\"; filename=\"03.jpg\"\r\ncontent-type: image/jpeg\r\n">}}, "commit"=>"update news", "id"=>"1-news-title"}
unpermitted parameter: :image
since have has_many :images, should have form.fields_for :images not form.fields_for :image
<%= form.fields_for :images |img| %> <%= img.file_field :image, multiple: true %> <% end%> update:
in case need explicitly pass record_object fields_for
<%= form.fields_for :images, @news.images.build |img| %> <%= img.file_field :image, multiple: true %> <% end%> also in order send multiple values image, should array in permitted params. should change news_params below
def news_params params.require(:news).permit(:title, :description, :category, images_attributes: [:id, :news_id, :_destroy, image: []]) end
Comments
Post a Comment