3

I'm building a simple app (Ruby 2.0.0 and Rails 4) where a user can create a project and for each project create multiple screens. When creating a screen the user can upload a screenshot, that refer to a its own model (I do this to handle multiple versions of the same screen).

When creating the screen, the screenshot doesn't seem to be created because of a permission problem. Here's the server log:

Processing by ScreensController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"kezaGADsaLmY/+zozgbjEe5UfdeqRPg58FCf1qzfHxY=", "screen"=>{"project_id"=>"24", "name"=>"Billing", "description"=>"This is the page where a user will enter their credit card information", "screenshot"=>{"image"=>#  <ActionDispatch::Http::UploadedFile:0x007fcdce25b2c0 @tempfile=#<Tempfile:/var/folders/pv/srwrv0qj35b0hsxkt42l_z500000gn/T/RackMultipart20131007-91790-tewse9>, @original_filename="fb-banner.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"screen[screenshot][image]\"; filename=\"fb-banner.png\"\r\nContent-Type: image/png\r\n">}}, "commit"=>"Create Screen"}
Unpermitted parameters: screenshot

These are my models:

Screen

class Screen < ActiveRecord::Base

  belongs_to :project
  has_many :screenshots

  validates :name, presence: true

  accepts_nested_attributes_for :screenshots

end

Screenshot

class Screenshot < ActiveRecord::Base

  belongs_to :screen

end

This is my screens_controller:

class ScreensController < ApplicationController
  before_action :set_screen, only: [:show, :edit, :update, :destroy]


  def index
    @screens = Screen.all
  end


  def show
  end


  def new
    @screen = Screen.new(:project_id => params[:project_id])
    @screen.screenshot.build
  end


  def edit
  end


  def create
    @screen = Screen.create(screen_params)
    if @screen.save
      flash[:notice] = "A new screen has been added to this project"
      redirect_to [@screen.project]
    else
      render :action => 'new'
    end
  end


  def update
    @screen = Screen.find(params[:id])
    if @screen.update_attributes(screen_params)
      flash[:notice] = "The screen has been successfully updated"
      redirect_to [@screen.project]
    else
      render :action => 'edit'
    end
  end

  def destroy
    @screen = Screen.find(params[:id])
    @screen.destroy
    flash[:notice] = "Successfully destroyed screen"
    redirect_to [@screen.project]
  end

  private
    def set_screen
      @screen = Screen.find(params[:id])
    end

    def screen_params
      params.require(:screen).permit(:project_id, :name, :description, screenshot_attributes: [ :id, :screen_id, :image, :version ])
    end
end

And finally this is the form:

<%= form_for @screen, :html => { :multipart => true } do |f| %>
  <% if @screen.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@screen.errors.count, "error") %> prohibited this screen from being saved:</h2>

      <ul>
      <% @screen.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.hidden_field :project_id %>
  </div>
  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_field :description %>
  </div>

  <%= f.fields_for :screenshot do |s| %>
    <%= s.hidden_field :screen_id, :value => @screen.id %>
    <%= s.hidden_field :version, :value => "1" %>
    <%= s.label :image %><br>
    <%= s.file_field :image %>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

I hope this is enough to help me spot the problem. I'm a newbie when it comes to programming, so any help is more than welcome.

1 Answers1

8

I recently worked through something similar, and this is what seemed to work...

Change your fields_for to plural:

<%= f.fields_for :screenshots do |s| %>

And also, make your params

def screen_params
  params.require(:screen).permit(:project_id, :name, :description, screenshots_attributes: [ :id, :screen_id, :image, :version ])
end

Also, you need to update your new action to make screenshots plural, like so:

def new
  @screen = Screen.new(:project_id => params[:project_id])
  @screen.screenshots.build
end
Helios de Guerra
  • 3,445
  • 18
  • 23
  • Thanks but now my file_field disappear. I did update the method new as well to `def new @screen = Screen.new(:project_id => params[:project_id]) @screen.screenshot.build end` – Davide Di Cillo Oct 07 '13 at 23:16
  • Sorry, I misspelled my previous comment. Even with `@screen.screenshots.build` the field doesn't show up. – Davide Di Cillo Oct 07 '13 at 23:41