Liondancer
Liondancer

Reputation: 16479

Understanding MVC for Rails 4 with image upload form

I am learning rails and I am trying to understand how the MVC model works with rails 4. I am practicing this by creating a form that will allow the user to upload an image with a name to the database. I am using CarrierWave to handle image storage in the database. This is what I have so far. As I am new to Rails 4, I'm not sure how all these parts connect together.

Here are my models for User and IncomePicture:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :rememberable, :validatable

  has_many :expense_pictures 
  has_many :income_pictures
end

class IncomePicture < ActiveRecord::Base
  belongs_to :user
  mount_uploader :image, ImageUploader
  has_one :income_text
end

Controllers:

class UserController < ApplicationController

  def create
    User.create(user_params)
  end

  private

    def user_params
      # required input for params
      # permit - returns a version of the params hash with ony the permitted attributes
      params.require(:user).permit(:name, :email, :password, :password_confirmation)
    end

end

class IncomePicturesController < ApplicationController

  def create
    # create IncomePicture object with params
    @income_picture = IncomePicture.new(IncomePicture_params)
    #
    if @income_picture.save
      flash[:notice] = "Income picture successfully uploaded"
    redirect_to 
  end

  private

    def IncomePicture_params
      params.require(:income_picture).permit(:image, :name)
    end
end

view for form:

<%= form_for @income_picture, :html => { :multipart => true } do |f| %>
    <p>
        <%= f.label :name %>
        <%= f.text_field :name %>
    </p>
    <p>
        <%= f.file_field :image %>
    </p>
    <p><%= f.submit %></p>
<% end %>

I'm not sure how to create a form that will store the upload to the logged in user. Currently only the user login portion works.

I am getting this error when I try to run rails s

First argument in form cannot contain nil or be empty

on the line

 -->  <%= form_for @income_picture, :html => { :multipart => true } do |f| %>
    <p>
        <%= f.label :name %>
        <%= f.text_field :name %>

Upvotes: 0

Views: 102

Answers (1)

jvnill
jvnill

Reputation: 29599

As the error says, the first argument of form_for cannot be nil or empty which means that @income_picture, the first argument, is most probably nil. So you have to ask why this variable is nil and where should I define it.

I'm assuming that the form is under app/views/income_pictures/new.html.erb which means that the most probable action corresponding to that view is the new action under IncomePicturesController.

Add a new action in the IncomePicturesController and define @income_picture

class IncomePicturesController < ApplicationController
  def new
    @income_picture = IncomePicture.new
  end

  ...
end

Upvotes: 1

Related Questions