Ruby on rails rails 4使用不同的控制器进行编辑

Ruby on rails rails 4使用不同的控制器进行编辑,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 4,我的个人资料/edit.html.erb中有以下表格 <%= form_for @customer, url: {controller: :customer, action: :update} do |f| %> <%= f.text_field :username, value: @customer.username%> <%= f.submit 'Update' %> <% end %> 当我提交表单时,获取路由错误 No route mat

我的个人资料/edit.html.erb中有以下表格

<%= form_for @customer, url: {controller: :customer, action: :update} do |f| %>
<%= f.text_field :username, value: @customer.username%>
<%= f.submit 'Update' %>
<% end %>
当我提交表单时,获取路由错误

No route matches [PATCH] "/customer/update"
我在route.rb中添加了以下行,但也遇到了相同的错误

resources :profiles
  resources :customers

调试此类问题的最简单方法是查看“rake路由”的输出,并查看输出中是否存在您试图调用的URL

如果我必须猜一猜…似乎您需要在补丁请求中传入客户的ID..类似于/customer/1/update,其中1是数据库中客户的ID

编辑:实际上只需取出表单的url部分即可。我认为这就是偏离轨道的原因。所以你的表单应该是

<%= form_for @customer do |f| %>


将controller::customer更改为controller::customer
s
在使用更新方法之前,您的更新操作从未定义@customer。在更新操作中尝试以下操作:

@customer = Customer.find session[:customer_id]
@customer.update(customer_params)
此外,您的客户参数设置不正确。要使其允许表单中的项目,请使用以下命令:

def profile_params
      params.require(:customer).permit(:username)
end

好的,一旦我尝试了上述格式,我的url就变成了
http://localhost:3000/customers/5
并将错误作为
ActionController::RoutingError发送到/customers/5/编辑未初始化的常量CustomersController
@设置,我认为您需要将控制器重命名为CustomersController…即。customers_controller.rb:)尝试获取错误,错误为
ActionController::ParameterMissing at/profile/edit param not found:customer
我的错误,使用def customer_param扫描您发布的客户表架构?名称、用户名、电子邮件、密码、电话号码、国家/地区、州。。。我的表模式。def customer_参数也会引发相同的错误此表单的目标是让用户更新其用户名?
def profile_params
      params.require(:customer).permit(:username)
end