Ruby rails 提交按钮不调用控制器

Ruby on rails submit button doesn't call the controller

我是 rails 上 ruby 的新手, 我想制作一个简单的文本字段,您可以在其中更改内容并将其存储在数据库中。但是提交按钮不会调用 Profiles 控制器。 "Foo" 从未被提出。

routes.rb:

Rails.application.routes.draw do
   devise_for :users
   get 'welcome/user'
   resources :profiles
end

user.html.erb:

...
<%@profile=Profile.find_by user_id: current_user.id%>
<%= form_for (@profile) do |f| %>
<%= f.text_field :name%>
<%= f.submit%>
<% end %>

profiles_controller.rb:

class ProfilesController < ActionController::Base
  def update
     raise "foo"
  end
end

你真的应该把它分开。

class ProfilesController < ActionController::Base
  def edit
    @profile = Profile.find_by(user_id: current_user.id)
  end

  def update
    raise "Foo"
  end
end

并创建相关的视图文件,edit.html.erb:

<%= form_for (@profile, method: ) do |f| %>
  <%= f.text_field :name %>
  <%= f.submit %>
<% end %>

至于问题本身,我建议您查看日志文件。在您的终端 运行 tail -f /path/to/app/log/* 然后尝试提交表单。如果您在此处看不到任何内容,那是不寻常的,如果是这种情况,请使用浏览器的开发人员工具来检查表单。你应该有这样的东西:

<form id="profile-form" action="/profiles/1" method="post">
...
</form> 

注意 action="/profiles/1,其中 1 是配置文件实体的 ID。