导轨不提交表格价值?

时间:2018-08-02 14:53:24

标签: ruby-on-rails

我有一个text_area的表格。我的问题是,当我单击提交时,它不会提交text_area值,而将其保留为空。我不知道为什么希望有人帮忙。

服务器日志

Started POST "/user/2/posts/2/recommendations" for 127.0.0.1 at 2018-08-03 09:14:46 +0200
Processing by RecommendationsController#create as JS
  Parameters: {"utf8"=>"✓", "recommendation"=>{"text"=>"hello_world"}, "commit"=>"submit", "user_id"=>"2", "post_id"=>"2"}
  User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/recommendations_controller.rb:6
  Post Load (0.0ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/recommendations_controller.rb:7
   (0.0ms)  begin transaction
  ↳ app/controllers/recommendations_controller.rb:17
  Post Load (0.0ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/recommendations_controller.rb:17
  recommendation Create (46.9ms)  INSERT INTO "recommendations" ("created_at", "updated_at", "user_id", "post_id") VALUES (?, ?, ?, ?)  [["created_at", "2018-08-03 07:14:47.832846"], ["updated_at", "2018-08-03 07:14:47.832846"], ["user_id", 2], ["post_id", 2]]
  ↳ app/controllers/recommendations_controller.rb:17
   (0.0ms)  rollback transaction
  ↳ app/controllers/recommendations_controller.rb:17
Completed 500 Internal Server Error in 141ms (ActiveRecord: 46.9ms)

ActiveRecord::NotNullViolation (SQLite3::ConstraintException: NOT NULL constraint failed: recommendations.text: INSERT INTO "recommendations" ("created_at", "updated_at", "user_id", "post_id") VALUES (?, ?, ?, ?)):

app/controllers/recommendations_controller.rb:17:in `create'

posts \ show.html.erb

<%= @post.user.username %>
<%= @post.content %>
    <%= form_for [@user, @post, @recommendation], method: :post, remote: true do |f| %>
      <%= f.text_area :text %>
      <%= f.submit "submit" %>
    <% end %>

recommendations_controller.rb

class RecommendationsController < ApplicationController
  def new
    @recommendation = recommendation.new
  end

  def create
    @user = current_user
    @post = Post.find(params[:post_id])
    @recommendation = current_user.recommendations.new(permit)
    @recommendation.user_id = @user.id
    @recommendation.post_id = @post.id

    if @recommendation.save
      respond_to do |format|
        format.html { redirect_back(fallback_location: 'back') }
        format.js
      end
    else
      redirect_to 'layouts/404'
    end
  end

  private

  def permit
    params.permit(:text)
  end
end

models / user.rb

has_many :recommendations

models / post.rb

has_many :recommendations

models / recommendation.rb

belongs_to :user
belongs_to :post

模式

create_table "recommendations", force: :cascade do |t|
  t.text "text", null: false
  t.integer "user_id"
  t.integer "post_id"
end

2 个答案:

答案 0 :(得分:0)

我相信您错误地允许了参数。您进行了params.permit(:text),但是表单中的推荐属性必须嵌套在:recommendation中的键params中。

尝试将permit(无论如何都不是该方法的最佳名称)更改为:

def permit
  params.require(:recommendation).permit(:text)
end

答案 1 :(得分:0)

使用以下代码的更改方法:

<template></template>
相关问题