2

I'm using Carrierwave to add an image field to a Rails nested model based on Ryan Bates revised #196 screencast. The Carrierwave bit is based on his screencast #253

The Question and Answer models are nested in a Categories model.

I can get the image to upload and to show on the Show page. But when I go back to the Edit view, there is no image and I still see the "No file chosen" text next to the Choose File button. I can go to the Show page and back and forth and it still appears in Show, but never in Edit. My users are going to think they have to upload a new image every time!

I have this line of code in my Show view:

<%= image_tag question.image_url(:thumb).to_s if question.image? %>

but if I try to put that into the _form view then I get an unknown local variable or method error.

I've tried a couple of if statements but haven't gotten it to work. I tried to put it all in the Edit view, but to no avail.

my _form view:

<%= form_for @category, :html => {:multipart => true} do |f| %>
  <% if @category.errors.any? %>
    <div class="error_messages">
      <h2><%= pluralize(@category.errors.count, "error") %> prohibited this category from being saved:</h2>
      <ul>
      <% @category.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div id="categorytitle" class="field">
    <%= f.label :name, "Category Name" %><br />
    <%= f.text_field :name %>
  </div>

  <%= f.fields_for :questions do |builder| %>
    <%= render 'question_fields', f: builder %>
  <% end %>
  <%= link_to_add_fields "Add Question", f, :questions %>

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

my _questions partial

<fieldset id="questionarea">
  <%= f.label :content, "Question" %>
  <%= f.text_field :content%>
  <hr>
  <div class="field">
    <p>
      <%= f.check_box :active, class: "pull-left" %>
      <%= f.label :active %>
      <span class="help-block">Turning questions on and off helps you customize the category.</span>
    </p>
  </div>
  <hr>

  <div class="field clearthat">
    <%= f.label :image, "Add an Image"%>
    <span class="help-block">Images will help non-readers understand the question.</span>
    <%= f.file_field :image %>
    <%= f.hidden_field :image_cache %>
  </div>
  <hr>
  <!-- <%= link_to "remove", '#', class: "remove_fields" %> -->
  <p class="clearthat">&nbsp;</p>
  <%= f.fields_for :answers do |builder| %>
    <%= render 'answer_fields', f: builder %>
  <% end %>
  <%= link_to_add_fields "Add Answer", f, :answers %>
</fieldset>

There is an _answer partial but that doesn't seem to figure into it.

Here's the error message I get when I try to put the image directly into my _question partial like so (snipped from the center of the _question code above):

<div class="field clearthat">
  <%= f.label :image, "Add an Image"%>
  <span class="help-block">Images will help non-readers understand the question.</span>
  <%= image_tag question.image_url(:thumb).to_s  if question.image?  %>
  <%= f.file_field :image %>
  <%= f.hidden_field :image_cache %>
</div>

Here's the error I get:

NameError in Categories#edit
undefined local variable or method `question'

My Category model:

class Category < ActiveRecord::Base
  attr_accessible :name, :questions_attributes
  has_many :questions
  accepts_nested_attributes_for :questions, allow_destroy: true
end

My Question model:

class Question < ActiveRecord::Base
  attr_accessible :content, :category_id, :answers_attributes, :active, :image, :image_cache
  belongs_to :category
  has_many :answers
  accepts_nested_attributes_for :answers, allow_destroy: true
  mount_uploader :image, ImageUploader
end

My Categories controller:

class CategoriesController < ApplicationController
  before_filter :signed_in_user, only: [:index, :edit, :update, :destroy]
  before_filter :correct_user,   only: [:edit, :update]
  before_filter :admin_user,     only: :destroy


def index
  @categories = Category.all
end

def show
  @category = Category.find(params[:id])
end

def new
  @category = Category.new
end

def create
  @category = Category.new(params[:category])
  if @category.save
    redirect_to @category, :notice => "Successfully created category."
  else
    render :action => 'new'
  end
end

def edit
  @category = Category.find(params[:id])
end

def update
  @category = Category.find(params[:id])
  if @category.update_attributes(params[:category])
    redirect_to @category, :notice  => "Successfully updated category."
  else
    render :action => 'edit'
  end
end

def destroy
  @category = Category.find(params[:id])
  @category.destroy
  redirect_to categories_url, :notice => "Successfully destroyed category."
end

private

  def signed_in_user
    unless signed_in?
      store_location
      redirect_to signin_path, notice: "Please sign in."
    end
  end

  def correct_user
    @user = User.find(params[:id])
    redirect_to(root_path) unless current_user?(@user)
  end

  def admin_user
    redirect_to(root_path) unless current_user.admin?
  end

end

Thanks to anyone who can help.

charliemagee
  • 683
  • 5
  • 19

1 Answers1

7

Perhaps my question wasn't asked very well because I didn't get any help. But I finally figured it out!

This is the line of code that was causing the problems:

<%= image_tag question.image_url(:thumb).to_s if question.image? %>

This is the line of code that worked:

<%= image_tag f.object.image_url(:thumb) %>

I'm not totally clear, but it appears that I was in my questions partial and calling a method called question and rails didn't know what I meant.

I found the answer in this stackoverflow QA: Rails Nested Forms With Images

Now I've got a working if statement so my users can change the image:

<% if f.object.image? %>
  <%= image_tag f.object.image_url(:thumb) %>
  <%= f.file_field :image %>
<% else %>
  <%= f.file_field :image %>
  <%= f.hidden_field :image_cache %>
<% end %>

Next I have to figure out how to change the value on the file_field button.

Community
  • 1
  • 1
charliemagee
  • 683
  • 5
  • 19