新手问题 一个简单的表单,一个按钮,一个字段,提交后数据还是没有保存上

wdrsam · 2015年10月07日 · 最后由 pathbox 回复于 2015年10月07日 · 1749 次阅读
控制器
def create
      @info = Info.new
 end

def new
     @info = Info.new(info_params)
  if @info.save
      flash[:success] = "Welcome!"
    else
      #
  end
 end

路由:

 root 'static_pages#home'

  get 'infos/:content' => 'infos#create'
  resources :infos, only:[:create, :destroy, :extract]

match '/create_message', to: 'infos#create', via: 'get'

页面里的表单

<% provide(:title, 'New Message') %>
<div class="row">
    <div class="span6 offset3">
    <%= form_for(@info) do |f| %>
        <%= f.label :content %>
        <%= f.text_field :content %>
        <br>
        <%= f.submit "Create a Message" , class: "btn" %>
    <% end %>
    </div>
</div>

点了提交后,从 rails console 里没有看到应该被保存到的字段,没有直接用 scaffold,想自己写出来就遇到问题了。求解答。

扫了一眼你的 Controller,你的 new 和 create 的实现为什么对调了?

你可以用在 def 里写 raise 'debug message'来跟踪流程。

一般提交对应的是 create,rake routes 看一下,不行的话,把 rake routes 结果贴个截图。

在你的 create action 中没有任务保存数据的逻辑呢?你 new,和 create 写反了

楼上 已经指出你的错误了

5 楼 已删除
需要 登录 后方可回复, 如果你还没有账号请 注册新账号