2012-06-16 41 views
2

我使用Carrierwave將圖像字段添加到基於Ryan Bates修訂的#196屏幕錄像的Rails嵌套模型。 Carrierwave位基於他的屏幕錄像#253無法在嵌套模型編輯頁面的我的_form視圖中顯示圖像

Question and Answer模型嵌套在Categories模型中。

我可以上傳圖片並在Show頁面上顯示。但是當我回到編輯視圖時,沒有圖像,我仍然看到選擇文件按鈕旁邊的「無文件選擇」文本。我可以前往Show頁面來回顯示,但它仍然出現在Show中,但從未出現在Edit中。我的用戶會認爲他們每次都必須上傳新圖片!

我有這行代碼在我的節目的看法:

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

,但如果我嘗試它放入_form視圖然後我得到一個未知的局部變量或方法的錯誤。

我試過幾個if語句,但還沒有得到它的工作。我試圖把它放在編輯視圖中,但無濟於事。

我_form觀點:

<%= 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 %> 

我_questions部分

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

有局部的_answer但似乎並沒有推測到它。

下面是錯誤消息我收到的時候我嘗試直接把圖像變成我的_question部分像這樣(剪斷從_question碼中心以上):

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

這是我得到的錯誤:

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

我的類別模型:

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

我的問題型號:

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 

我的類別控制器:

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 

由於任何人誰可以提供幫助。

回答

7

也許我的問題沒有問得很好,因爲我沒有得到任何幫助。但我終於明白了!

這是代碼所造成的問題行:

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

這是代碼工作的一行:

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

我不完全清楚,但現在看來,我在我的問題部分,並調用一個稱爲問題的方法,rails不知道我的意思。

我發現這個計算器QA答案:Rails Nested Forms With Images

現在,我已經有了一個工作,如果語句,因此我的用戶可以改變圖像:

<% 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 %> 

接下來,我要弄清楚如何更改file_field按鈕上的值。

相關問題