ruby-on-rails - rails : How to store a JSON object from a form and query it?

标签 ruby-on-rails json forms postgresql querying

我是 Rails 的新手,需要帮助。我希望我的新帖子表单允许用户将文本从 JSON 文件复制并粘贴到 text_area 中,并将其作为 JSON 对象存储在我的 Postgresql 数据库中。然后我希望能够在 rails 控制台或仅在 psql 中查询 JSON 对象。

这是我的表格:

<h1>New Post</h1>
<hr>

<div align="center"><%= form_for (@post) do |f| %>
    <div class="field">
        <%= f.label :title %>
        <%= f.text_field :title, :required => true %>
    </div>
    <br>
    <div class="field"> 
        <%= f.label :description %>
        <%= f.text_area :description, :required => true %>
    </div>
    <br>
    <div class="field">
        <%= f.file_field :json, :required => true %>
        <%= f.hidden_field :json_cache %>
    </div>
    <br>
    <div class="field">
                <%= f.label "JSON" %>
                <%= f.text_area :data, :required => true %>
        <%= f.hidden_field :user_id , :value => current_user.id %>
        </div>
        <br>
    <%= f.hidden_field :user_id , :value => current_user.id %>

    <div class="actions">
        <%= f.submit %>
    </div>
<% end %></div>

这是我的帖子 Controller :
class PostsController < ApplicationController
  def edit
    @post = Post.find(params[:id])
  end
  def update
    @post = Post.find(params[:id])
    @post.update_attributes(post_params)
    redirect_to post_path(@post)
  end
  def new
    @post = Post.new
    @post.user_id = current_user.id
  end

  def index
    @posts = Post.all
  end

  def show
    @post = Post.find_by_id(params[:id])
  end

  def create
    @post = Post.new(post_params)
    if @post.save
        flash[:success] = "Success!"
        redirect_to post_path(@post)
    else
        flash[:error] = @post.errors.full_messages
        redirect_to new_post_path
    end
  end
  def destroy
    @post = Post.find(params[:id])
    @post.destroy
    redirect_to new_post_path
  end

    private
    def post_params
        params.require(:post).permit( :json, :description, :title, :data, :user_id )
    end
end

这是我的数据库架构:
ActiveRecord::Schema.define(version: 20161127203635) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "posts", force: :cascade do |t|
    t.text     "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.string   "title"
    t.integer  "user_id"
    t.string   "json"
    t.json     "data"
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "first_name"
    t.string   "last_name"
    t.string   "username"
    t.date     "birthday"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

end

当我去查询数据库中的 JSON 时,我没有得到任何返回。

我做:psql mydatabase

然后我尝试: SELECT data->>'name' AS name FROM posts

但它不会返回任何东西。

这是一个 JSON 文件,它应该存储在我的数据库中的 :data 列中:
{"widget": {
    "debug": "on",
    "window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "width": 500,
        "height": 500
    },
    "image": { 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": "center"
    },
    "text": {
        "data": "Click Here",
        "size": 36,
        "style": "bold",
        "name": "text1",
        "hOffset": 250,
        "vOffset": 100,
        "alignment": "center",
        "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
    }
}}

那我做错了什么?我需要能够查询 JSON。

最佳答案

像这样形成你的帖子参数

def post_params
    params.require(:post).permit(:description, :title, :user_id, json: {}, data: {})
end

关于ruby-on-rails - rails : How to store a JSON object from a form and query it?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40835024/

相关文章:

ruby-on-rails - 在 Heroku 上部署 Activeadmin 时为 "uninitialized constant AdminUser"

json - 对于 JSON 操作,您会推荐哪个 Perl 模块?

javascript - 无法将表单数据推送到 JSON 文件中

javascript - 使用 JavaScript 将表单上的按钮定位为按下

javascript - JS 表单验证在 IE 中不起作用

ruby-on-rails - Rails 4 - 插入重复的 ActiveRecord 数据

ruby-on-rails - Rails 中的 LocalJumpError(未给出 block )

mysql - 如何保护 Rails find_by_SQL 语句免受 SQL 注入(inject)?

json - 如何使用公用 key 组合并附加两个json文件而不丢失其他数据

php - 如何解码来自android的PHP传递中的JSON字符串?