ruby on rails 4 - Using simple_forms simple_field_for with a multipart file uploader for Carrierwave -


i creating blog allows me upload multiple image files when create blog post. using rails 4 simple form gem. unable file input allow selection of multiple files, though upload of single file works fine.

here form:

<%= simple_form_for(@post, :html => { :multipart => true }) |f| %>   <% if @post.errors.any? %>     <div id="error_explanation">       <h2, class: "white"><%= pluralize(@post.errors.count, "error") %> prohibited post being saved:</h2>        <ul>       <% @post.errors.full_messages.each |message| %>         <li, class: "white"><%= message %></li>       <% end %>       </ul>     </div>   <% end %>    <%= f.input :title %>   <%= f.input :subtitle %>   <%= f.input :author %>   <%= f.input :content, as: :text %>   <%= f.input :publish, as: :select %>   <%= f.association :tags, as: :check_boxes %>    <div id="tabs">     <ul>       <li><a href="#tabs-1">upload images</a></li>       <li><a href="#tabs-2">link video</a></li>       <li><a href="#tabs-3">quote</a></li>     </ul>     <div id="tabs-1">         <%= f.simple_fields_for :post_attachments |p| %>          <div class="field">            <%= p.input :media, as: :file, :multiple => true, name: "post_attachments[media][]" %>          </div>        <% end %>        <div class="actions"></br>           <%= f.button :submit %>        </div>     </div>     <div id="tabs-2">       <div class="field">         <%= f.label :video_link %><br>         <%= f.text_field :video_link %>       </div>       <div class="actions"></br>           <%= f.button :submit %>       </div>     </div>     <div id="tabs-3">       <div class="field">         <%= f.label :quote_text %><br>         <%= f.text_field :quote_text %>       </div>       <div class="field">         <%= f.label :quote_author %><br>         <%= f.text_field :quote_author %>       </div>       <div class="field">         <%= f.label :quote_source %><br>         <%= f.text_field :quote_source %>       </div>       <div class="actions"></br>           <%= f.button :submit %>       </div>     </div>   </div>  <script>   $(function() {     $("#tabs").tabs();   }); </script> <% end %> 

i suspect "multipart" attribute not entered properly. can't seem find documentation on other statement on simple_form wiki:

"wrapper use simple form inside default rails form. works in same way fields_for rails helper, change builder use simpleform::formbuilder."

i don't understand here. appreciate guidance on issue.

update: here controller code

posts_controller.rb

class postscontroller < applicationcontroller   before_action :set_post, only: [:show, :edit, :update, :destroy]   before_action :authenticate_user!, only: [:index, :edit, :update, :destroy]   layout :resolve_layout    respond_to :html    def index     @posts = post.all     respond_with(@posts)   end    def list     @posts = post.all     respond_with(@posts)   end    def show     @post_attachments = @post.post_attachments.all     respond_with(@post)   end    def new     @post = post.new     @post_attachment = @post.post_attachments.build     respond_with(@post)   end    def edit   end    def create     @post = post.new(post_params)      respond_to |format|       if @post.save         if (@post_attachments != nil)           params[:post_attachments]['media'].each |a|             @post_attachment = @post.post_attachments.create!(:media => a, :post_id => @post.id)           end         end          format.html { redirect_to @post, notice: 'post created.'}       else         format.html { render action: 'new' }       end     end     # @post.save     # respond_with(@post)   end    def update     @post.update(post_params)     respond_with(@post)   end    def destroy     @post.destroy     respond_with(@post)   end    private     def set_post       @post = post.find(params[:id])     end      def post_params       params.require(:post).permit(:title, :subtitle, :author, :content, :publish, :quote_author, :quote_text, :quote_source, :video_link, post_attachments_attributes: [:id, :post_id, :media])     end      def resolve_layout       case action_name       when "edit", "new", "create", "index"         "full_width"       when "show", "list"         "blog_rt"       else         "base"     end   end end 

posts_attachments_controller.rb

  class postattachmentscontroller < applicationcontroller   before_action :set_post_attachment, only: [:show, :edit, :update, :destroy]    respond_to :html    def index     @post_attachments = postattachment.all     respond_with(@post_attachments)   end    def show     respond_with(@post_attachment)   end    def new     @post_attachment = postattachment.new     respond_with(@post_attachment)   end    def edit   end    def create     @post_attachment = postattachment.new(post_attachment_params)     @post_attachment.save     respond_with(@post_attachment)   end    def update     @post_attachment.update(post_attachment_params)     respond_with(@post_attachment)   end    def destroy     @post_attachment.destroy     respond_with(@post_attachment)   end    private     def set_post_attachment       @post_attachment = postattachment.find(params[:id])     end      def post_attachment_params       params.require(:post_attachment).permit(:post_id, :media)     end end 

here models:

post.rb

class post < activerecord::base     has_many :post_attachments     accepts_nested_attributes_for :post_attachments end 

post_attachment.rb

class postattachment < activerecord::base     mount_uploader :media, mediauploader     belongs_to :post  end 

try changing line

<%= p.input :media, as: :file, :multiple => true, name: "post_attachments[media][]" %> 

to

<%= p.input :media, as: :file, :multiple => true, name: "post[post_attachments_attributes][][media]" %> 

update:

you should change :media :media => [] in post_params method of posts_controller save multiple values.

def post_params    params.require(:post).permit(:title, :subtitle, :author, :content, :publish, :quote_author, :quote_text, :quote_source, :video_link, post_attachments_attributes: [:id, :post_id, :media => []]) end 

source


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -