简体   繁体   English

更新Postgres.app和重新创建数据库后,无法创建和更新记录

[英]Cannot create and update records after updating Postgres.app and recreating db

I'm a ruby on rails noobie and have upgraded postgress app to 9.4.2 (from 9.3.5) and ruby to 2.2.2 (from 2.2.1). 我是Rails Noobie上的红宝石,并且已将postgress应用升级到9.4.2(从9.3.5)和红宝石升级到2.2.2(从2.2.1)。 After the update, I had to recreate and migrate the db from fresh. 更新之后,我不得不重新创建数据库并从中迁移数据库。

I can't create and update any records anymore throughout the application (ie all controllers/views). 我无法在整个应用程序(即所有控制器/视图)中创建和更新任何记录。 It used to work before. 它曾经工作过。 The output I'm getting is: 我得到的输出是:

Started GET "/expense_claims/new?utf8=%E2%9C%93&authenticity_token=iVEJfsXCoIKdngHRe6Zviqz6hqo8yT%2FAzpcEv73kYu47Q2Bdczqdm8RZZJyv%2BfJSVBexfglVczOXMQ1tXzJt%2BQ%3D%3D&expense_claim%5Bname%5D=Some+title+for+the+claim+comes+here&commit=Spesenabrechnung+erstellen" for ::1 at 2015-05-25 19:19:16 +0200
Processing by ExpenseClaimsController#new as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"iVEJfsXCoIKdngHRe6Zviqz6hqo8yT/AzpcEv73kYu47Q2Bdczqdm8RZZJyv+fJSVBexfglVczOXMQ1tXzJt+Q==", "expense_claim"=>{"name"=>"Some title for the claim comes here"}, "commit"=>"Spesenabrechnung erstellen"}
  Rendered expense_claims/_form.html.slim (2.3ms)
  Rendered expense_claims/new.html.slim within layouts/application (4.8ms)
  Rendered layouts/_sidebar.html.slim (0.7ms)
  Rendered layouts/_flashes.html.slim (0.1ms)
  Rendered layouts/_topbar.html.slim (0.1ms)
Completed 200 OK in 947ms (Views: 945.9ms | ActiveRecord: 0.0ms)

It looks like it is a get-request instead of a post-request. 看起来这是一个get请求而不是post-request。 The url after submitting is: http://localhost:3000/expense_claims/new?utf8=%E2%9C%93&authenticity_token=iVEJfsXCoIKdngHRe6Zviqz6hqo8yT%2FAzpcEv73kYu47Q2Bdczqdm8RZZJyv%2BfJSVBexfglVczOXMQ1tXzJt%2BQ%3D%3D&expense_claim[name]=Some+title+for+the+claim+comes+here&commit=Spesenabrechnung+erstellen 提交后的网址为: http:// localhost:3000 / expense_claims / new?utf8 =%E2%9C%93&authenticity_token = iVEJfsXCoIKdngHRe6Zviqz6hqo8yT%2FAzpcEv73kYu47Q2Bdczqdm8RZZJyv %% 2BftXense_X_X_X_X_X_Q_X_Q_Q_Q_Q_%2BfJtX_J_Vy_%2BfT_TyS_Y_V%Q2BfJtX_J_Vy_%2Bfj如权利要求自带+ +此处&提交= Spesenabrechnung + ERSTELLEN

For completeness, here an extract of one of the controllers: Form: 为了完整起见,这里摘录了其中一个控制器:表格:

= form_for(@expense_claim) do |f|

  - if @expense_claim.errors.any?
    ul.error-messages
      - @expense_claim.errors.full_messages.each do |message|
        li.error-message = message

  .field
    = f.label :name
    = f.text_field :name, placeholder: 'Juni Spesenabrechnung', maxlength: 100, class: 'form-control'
    p.help-block
      | Ein beschreibender Titel für die Spesenabrechnung, damit Du Dich erinnern kannst, was sie enthält.

  .actions
    = f.submit class: 'btn btn-primary'
    = link_to 'Abbrechen', expense_claims_path, class: 'btn btn-link'

Controller: 控制器:

  def new
    @expense_claim = ExpenseClaim.new
  end

  def create
    @expense_claim = ExpenseClaim.new(expense_claim_params)

    if @expense_claim.save
      redirect_to @expense_claim, notice: 'Spesenabrechnung wurde erstellt'
    else
      render :new
    end
  end

Model: 模型:

class ExpenseClaim < ActiveRecord::Base
  has_many :receipts
  has_many :mileages

  validates :name, presence: true

end

resources: 资源:

resources :expense_claims

I can create and update records through the terminal without any problems and for all controllers. 我可以通过终端为所有控制器创建和更新记录,而不会出现任何问题。 After trying to solve this for hours, I'm at a loss why it suddenly doesn't work anymore. 在尝试解决了几个小时后,我无所适从,为什么它突然不再起作用了。 Thanks. 谢谢。

The first thing to do is to verify that the create controller action is actually being called, since that's the only place that database changes are being made right? 首先要做的是验证create controller动作是否真正被调用,因为这是唯一对数据库进行更改的地方? Right after def create , add a line like the following: def create ,立即添加如下一行:

puts "#create called with params: #{params}"

and verify that this line appears in your Rails server console output. 并验证此行是否出现在Rails服务器控制台输出中。

I'm confused by the output you pasted (at top), because that's a GET request to the new action. 我对您粘贴的输出(在顶部)感到困惑,因为这是对new操作的GET请求。 Remember Rails resourceful routing defaults expect 2 steps to creating something: 请记住,Rails的资源路由默认设置需要两个步骤来创建:

  1. GET #new shows the form for preparing the data on that resource GET #new显示用于准备该资源上的数据的表单
  2. When the form is submitted, it POSTs to #create where the new animal is actually created. 提交表单后,它将发布到#create实际创建新动物的位置。

It looks like either you're copying & pasting the wrong section of console output, or your form is never getting submitted. 看起来您正在复制并粘贴控制台输出的错误部分,或者表单从未提交。

Also, go to GET #new and inspect the HTML code for the form. 另外,转到GET #new并检查表单的HTML代码。 In particular you should see method="post" and action="/expense_claims [NOT expense_claims/new]" . 特别是,您应该看到method="post"action="/expense_claims [NOT expense_claims/new]"

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM