【发布时间】: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 模型中创建一个方法,但它仍然显示上述错误。
todo_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