Question

I am trying to understand Rails' field_for, specifically what should go into the controller for nested resources. My issue is that when I create a comic with comic pages through the Comic form, the page's image are not saved.

I have Users, Comics, and ComicPages. Here are the models:

class User < ActiveRecord::Base
  has_many :comics
  has_many :comic_pages, through: :comics
end

class Comic < ActiveRecord::Base
    belongs_to :user
    has_many :comic_pages, :dependent => :destroy
    accepts_nested_attributes_for :comic_pages
end

class ComicPage < ActiveRecord::Base
    belongs_to :comic
end

Here is the form for Comic, where I also want to add comic_pages:

 <%= form_for ([@user, @comic]) do |f| %>
    <%= f.text_field :title %>
    <%= f.fields_for :comic_pages do |comic_page| %>
        <%= comic_page.file_field :comic_page_image %>
    <% end %>
    <div class="actions">
      <%= f.submit %>
    </div>
  <% end %>

I am confused about the comics_controller (new and create actions). How can I pass comic_page params to this controller???

  def new
    @user = current_user
    @comic = @user.comics.new
    @comic.comic_pages.build
  end

  def create
    @user = current_user
    @comic = @user.comics.new(comic_params)
    @comic.comic_pages.build

    respond_to do |format|
      if @comic.save
        format.html { redirect_to @user, notice: 'Comic was successfully created.' }
        format.json { render action: 'show', status: :created, location: @user }
      else
        format.html { render action: 'new' }
        format.json { render json: @comic.errors, status: :unprocessable_entity }
      end
    end
  end

private
# Use callbacks to share common setup or constraints between actions.
def set_comic
  @comic = Comic.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def comic_params
  params.require(:comic).permit(:title, :synopsis)
end

def comic_page_params
  params.require(:comic_page).permit(:comic_page_image, :comic_image_file_name)
end

Many thanks!

--- EDIT ---

After the answer for the params, I used it to create the following create action:

def create
    @user = current_user
    @comic = @user.comics.new(comic_params)

    i = 0
    until i = 1
      @comic_page = @comic.comic_pages.new(comic_params[:comic_pages_attributes]["#{i}"])
      @comic_page.save
      i += 1
    end

    respond_to do |format|
      if @comic.save
        ...
      end
    end
  end
Was it helpful?

Solution

You need to permit those fields from comic_pages that you want to save through in the comic_params section of your controller

params.require(:comic).permit(:title, :synopsis, comic_pages_attributes: [:comic_page_image])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top