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:

&lt;%= image_tag question.image_url(:thumb).to_s if question.image? %&gt;


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:

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

  &lt;div id="categorytitle" class="field"&gt;
    &lt;%= f.label :name, "Category Name" %&gt;&lt;br /&gt;
    &lt;%= f.text_field :name %&gt;
  &lt;/div&gt;

  &lt;%= f.fields_for :questions do |builder| %&gt;
    &lt;%= render 'question_fields', f: builder %&gt;
  &lt;% end %&gt;
  &lt;%= link_to_add_fields "Add Question", f, :questions %&gt;

  &lt;div class="actions"&gt;
    &lt;%= f.submit %&gt;
  &lt;/div&gt;
&lt;% end %&gt;


my _questions partial

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

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


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):

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


Here's the error I get:

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


My Category model:

class Category &lt; ActiveRecord::Base
  attr_accessible :name, :questions_attributes
  has_many :questions
  accepts_nested_attributes_for :questions, allow_destroy: true
end


My Question model:

class Question &lt; 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 &lt; 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 =&gt; "Successfully created category."
  else
    render :action =&gt; '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  =&gt; "Successfully updated category."
  else
    render :action =&gt; 'edit'
  end
end

def destroy
  @category = Category.find(params[:id])
  @category.destroy
  redirect_to categories_url, :notice =&gt; "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.


Question: Generating RSA-SHA1 signatures with JavaScript
Answer: I tried both the openssl command and the JS library you mentioned above, and the results are consistent. The signatures that I get from both ways are the same.

One thing I noticed from your post is that, the base64 encoded result generated from the library is way too long and looks wrong. Is it possible that you are not base64-encoding the binary signature?

Could you try this code for getting the base64 encoded string of the signature?

function doSign() {
  var rsa = new RSAKey();
  rsa.readPrivateKeyFromPEMString(document.form1.prvkey1.value); //replace with your private key
  var hSig = rsa.signString("x", "sha1");
  var base64_encoded_signature = hex2b64(hSig);
}


If you compare the value of "base64_encoded_signature" with what you get from the openssl command, they should be the same.