【问题标题】:undefined method `picture' for nil:NilClass using carrierwavenil的未定义方法“图片”:使用载波的NilClass
【发布时间】:2014-10-16 12:22:11
【问题描述】:

我正在我的 Rails 应用程序中制作一个表单,人们可以选择添加图像,并且我正在使用“carrierwave”,但在编辑页面上出现未定义的方法错误。这是表单的代码:

<%= title "Add Item to #{@todo_list.title}" %>

<%= form_for [@todo_list, @todo_item], builder: FoundationFormBuilder do |form| %>
  <%= render partial: 'form', locals: { form: form } %>

  <%= form.file_field :picture %>

<% end %>

在这里我可以看到上传按钮,它工作正常,但在编辑页面上我收到上述错误。我的编辑页面的代码:

<%= title "Editing Todo Item" %>

<%= form_for [@todo_list, @todo_item], builder: FoundationFormBuilder do |form| %>
   <%= render partial: 'form', locals: { form: form } %>
<% end %>

<div class="row">
  <div class="small-12 columns">
    <%= link_to "Delete", todo_list_todo_item_path(@todo_list, @todo_item), method: :delete, data: { confirm: "Are you sure?" }, class: "button radius expand alert" %>
  </div>

  <%= @todo_item.picture %>

</div>

为什么这显示一个未定义的方法错误。我尝试在我的 todo_item 模型中创建一个方法,但它仍然显示上述错误。

tod​​o_item 的控制器:

class TodoItemsController < ApplicationController

  before_action :require_user
  before_action :find_todo_list
  before_action :set_back_link, except: [:index]

  def index
    go_back_link_to todo_lists_path
  end

  def new
    @todo_item = @todo_list.todo_items.new
  end

  def create
    @todo_item = @todo_list.todo_items.new(todo_item_params)
    if @todo_item.save
      flash[:success] = "Added todo list item."
      redirect_to todo_list_todo_items_path
    else
      flash[:error] = "There was a problem adding that todo list item."
      render action: :new
    end
  end

  def edit
    @todo_item = @todo_list.todo_items.find(params[:id])
  end

  def update
    @todo_item = @todo_list.todo_items.find(params[:id])
    if @todo_item.update_attributes(todo_item_params)
      flash[:success] = "Saved todo list item."
      redirect_to todo_list_todo_items_path
    else
      flash[:error] = "That todo item could not be saved."
      render action: :edit
    end
  end

  def destroy
    @todo_item = @todo_list.todo_items.find(params[:id])
    if @todo_item.destroy
      flash[:success] = "Todo list item was deleted."
    else
      flash[:error] = "Todo list item could not be deleted."
    end
    redirect_to todo_list_todo_items_path
  end

  def complete
    @todo_item = @todo_list.todo_items.find(params[:id])
    @todo_item.toggle_completion!
    redirect_to todo_list_todo_items_path, notice: "Todo item updated."
  end

  def url_options
    { todo_list_id: params[:todo_list_id] }.merge(super)
  end

  private

  def set_back_link
    go_back_link_to todo_list_todo_items_path(@todo_list)
  end

  def find_todo_list
    @todo_list = current_user.todo_lists.find(params[:todo_list_id])
  end

  def todo_item_params
    params[:todo_item].permit(:content)
  end

end

【问题讨论】:

  • @todo_items 为 nil 你确定它不应该是@todo_item?
  • 已经试过了,没有变化
  • 那么你应该在上面的代码中显示@todo_items 的位置
  • 你说得对,它应该是@todo_item,我已经改变了它,但它仍然不起作用
  • 让我编辑我的问题

标签: ruby-on-rails ruby forms carrierwave image-uploading


【解决方案1】:

要显示您的图像,您应该更改

<%= @todo_item.picture %>

<%= image_tag(@todo_item.picture_url) %>

【讨论】:

  • 仍然没有显示图像。我不明白发生了什么:/
  • @Rohit 你有没有遇到任何错误,你确定你有那个 todo_item 的图片吗?
  • 我没有收到任何错误,如何确定?我的意思是如何检查?但是页面的来源显示&lt;img src="" /&gt;这是什么意思吗?
  • @Rohit 是的,您的 to_do 没有图像。通过一些 GUI 工具或在 Rails 控制台中查询您的数据库来检查您的数据库。你还要怎么检查呢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多