2016-06-10 64 views
0

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

的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 
+0

看看你的服务器,看看是什么动作叫做 – Ursus

+0

'<%@ profile = Profile.find_by user_id:current_user.id%>' - 那东西绝对属于控制器。 –

+0

也许你需要为'form_for'帮手提供动作。 –

回答

0

你真的应该分开了这一点。

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。

+0

谢谢,“foo” - 消息实际上被提出,我期望看到它在浏览器中。 –