Created
July 30, 2014 11:54
-
-
Save rishighan/6dc90ce0df9f09fbacbf to your computer and use it in GitHub Desktop.
Cocoon not saving all fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div class="well"> | |
| <%= f.label :attachments %> | |
| <%= f.file_field :picture %> | |
| <%= link_to_remove_association 'Remove image', f, {wrapper_class: "well"} %> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <%= form_for(@post) do |f| %> | |
| <% if @post.errors.any? %> | |
| <div class="error_explanation"> | |
| <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2> | |
| <ul> | |
| <% @post.errors.full_messages.each do |msg| %> | |
| <li><%= msg %></li> | |
| <% end %> | |
| </ul> | |
| </div> | |
| <% end %> | |
| <p id="attachments"> | |
| <%= f.fields_for :attachments do |at| %> | |
| <%= render 'attachment_fields', :f => at %> | |
| <%= link_to_add_association 'Add another image', f, :attachments %> | |
| <% end %> | |
| </p> | |
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Attachment < ActiveRecord::Base | |
| include Rails.application.routes.url_helpers | |
| belongs_to :imageable | |
| has_attached_file :picture, | |
| :styles =>{ :medium => "660x", | |
| :thumb => "150x" | |
| }, | |
| :url => "/attachments/pictures/:style/:basename.:extension", | |
| :path =>"#{Rails.root}/public/attachments/pictures/:style/:basename.:extension" | |
| validates_attachment_content_type :picture, :content_type => /\Aimage\/.*\Z/ | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Post < ActiveRecord::Base | |
| has_many :attachments, as: :imageable, dependent: :destroy | |
| accepts_nested_attributes_for :attachments, allow_destroy: true | |
| has_and_belongs_to_many :categories, join_table: :categories_posts | |
| accepts_nested_attributes_for :categories | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class PostsController < ApplicationController | |
| before_action :set_post, only: [:show, :edit, :update, :destroy] | |
| layout "admin" | |
| # GET /posts/new | |
| def new | |
| @post = Post.new | |
| @post.attachments.build | |
| end | |
| # GET /posts/1/edit | |
| def edit | |
| end | |
| # POST /posts | |
| # POST /posts.json | |
| def create | |
| @post = Post.new(post_params) | |
| respond_to do |format| | |
| if @post.save | |
| format.html { redirect_to @post, notice: 'Post was successfully created.' } | |
| format.json { render action: 'show', status: :created, location: @post } | |
| else | |
| format.html { render action: 'new' } | |
| format.json { render json: @post.errors, status: :unprocessable_entity } | |
| end | |
| end | |
| end | |
| private | |
| # Use callbacks to share common setup or constraints between actions. | |
| def set_post | |
| @post = Post.find(params[:id]) | |
| end | |
| def post_params | |
| params.require(:post).permit(:title, :content, :excerpt, {:category_ids=>[]}, attachments_attributes:[:id, :picture, :_destroy]) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment