作为 Rails 新手,我浏览了 Hartl 教程并尝试进行一些更改。我想允许用户编辑帖子(我称之为路线),并且当我去编辑路线时收到此错误:
else
object = record.is_a?(Array) ? record.last : record
raise ArgumentError, "First argument in form cannot contain nil or be empty" unless object
object_name = options[:as] || model_name_from_record_or_class(object).param_key
apply_form_for_options!(record, object, options)
end
控制器:
class RoutesController < ApplicationController
# before_action :logged_in_user#, only: [:create, :destroy, :edit, :new]
def index
@routes = Route.paginate(page: params[:page])
end
def create
@routes = current_user.routes.build(route_params)
if @routes.save
flash[:success] = "Route created!"
redirect_to '/routes#index'
else
render 'new'
end
end
def destroy
end
def show
@routes = Route.find(params[:id])
end
def edit
end
def update
if @routes.update_attributes(routes_params)
flash[:success] = "Route updated"
redirect_to @routes
else
render 'edit'
end
end
private
def route_params
params.require(:route).permit(:title, :content, :picture)
end
end
这是 edit.html.erb 文件
<% provide(:title, "Edit route") %>
<h1>Update your route</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for @route do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control' %>
<%= f.label :content %>
<%= f.text_field :content, class: 'form-control' %>
<h4>Change your image.</h4>
<span class="picture">
<%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
</span>
<%= f.submit "Save changes", class: "btn btn-primary" %>
<% end %>
</div>
</div>
感谢您的帮助!
您必须在控制器的编辑操作中设置 @route 变量。
def edit
@route = Route.find(params[:id])
end
应该可以做到这一点。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)