我试图在rails上编辑ruby,但它向我显示了关于编辑的参数错误。我对这个问题很困惑。
然后,我试图将不同的参数放入index.html.erb但是,它仍然不起作用。例如m.id
和m
这是index.html.erb
<% @methodtypes.each do|m| %>
<tr>
<td><%=m.name %></td>
<td><%=m.desp %></td>
</tr>
<%= link_to "Edit", edit_method_types_path(m.id) %>
<% end %>
<%= link_to "Create Method", new_method_types_path %>
这是我的控制器文件:
class MethodTypesController < ApplicationController
def index
@methodtypes = MethodType.all
end
def show
@methodtype = MethodType.find_by_id(params[:id])
end
def create
@methodtype = MethodType.new(method_params)
@methodtype.save
if @methodtype.save
redirect_to method_types_path
else
render :new
end
end
def edit
@methodtype = MethodType.find_by_id(params[:id])
end
def new
@methodtype = MethodType.new
end
private
def method_params
params.require(:method_type).permit(:name, :desp)
end
这是我的编辑页面,即edit.html.erb:
<%= form_for @methodtype do |f| %>
<div>
<%= f.label :name %>
<%= f.text_area :name %>
</div>
<div>
<%= f.label :desp %>
<%= f.text_field :desp %>
</div>
<%= f.submit %>
<% end %>
结果应该表明我可以编辑我的文本。但是,它在MethodTypes#edit中显示了ArgumentError。有人可以给我一些建议,我不知道如何解决这个问题.....
错误的编辑网址路径
它应该是<%= link_to“Edit”,edit_method_type_path(m.id)%>而不是<%= link_to“Edit”,edit_method_types_path(m.id)%>
还要检查你的路线文件似乎你正在定义
resource: method_types
改成
resources: method_types
<%= link_to "Edit", edit_method_types_path(m.id) %>
应该是<%= link_to "Edit", edit_method_type_path(m) %>
,注意类型是单数。
运行rails routes -g method_type
确认它。
另外,在控制器中将MethodType.find_by_id(params[:id])
更改为MethodType.find(params[:id])
。
顺便说一句,你在save
方法中两次调用create
:
def create
@methodtype = MethodType.new(method_params)
@methodtype.save # delete this line
if @methodtype.save
redirect_to method_types_path
else
render :new
end
end