Rails:如何从表单中存储JSON对象并进行查询?

时间:2016-11-27 23:16:34

标签: 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>

这是我的帖子控制器:

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-&gt;&gt;&#39; name&#39; AS名称来自帖子

但它没有任何回报。

这是一个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。

1 个答案:

答案 0 :(得分:0)

像这样形成您的帖子参数

PS> (Get-Date).ToString('dd-MM-yyyy_hh+mm+ss') -replace '\+', [char] 0xA789
06-11-2020_09꞉17꞉25