简体   繁体   English

在 Rails 中,如何从 JavaScript 查询我的数据库以便保存查询值?

[英]In Rails, how do I query my database from JavaScript so I can save the value of the query?

I would like to make a query given the id of my book in javascript and not fetch all the books from the database, for example:我想根据我在 javascript 中的图书 ID 进行查询,而不是从数据库中获取所有图书,例如:

I usually do it like this:我通常这样做:

In my book controller , I make a query the collection of books and then pass it to the view through a variable:在我的书 controller中,我查询了书籍的集合,然后通过变量将其传递给视图:

# books_controllers.rb
before_action :set_books
... 
def set_books
  @books = Book.all # here is my books collection
end

In my form view I define a variable to store the collection of books, in a books_info variable:在我的表单视图中,我在books_info变量中定义了一个变量来存储书籍的集合:

#books/form
javascript:
   let books_info = #{@books.to_json.html_safe};

And finally, the collection of books is scanned and compared with the id that the client gives me to find the specific information of said book.最后,对藏书进行扫描,并与客户给我的id进行比对,找到该书的具体信息。

    Array.from(books_info).forEach((element, index) => {
      if (element["book_id"] === client_book_id){
        new_book_id = element;
      }
    });

The problem with doing all that is that I compromise all the book information in views这样做的问题是我妥协了视图中的所有书籍信息

Simply what I am looking for is given the id of the client ( client_book_id ) to do this in javascript只是我正在寻找的是在 javascript 中给出了客户端的 ID ( client_book_id ) 来执行此操作

javascript:
  let client_book_id = #{Book.find(´client_book_id´).to_json.html_safe} # This not work but I try to got it.

If you could help me I would be very grateful.如果你能帮助我,我将不胜感激。

  1. Add the book ID to the query string of the URL, eg ?book_id=123在 URL 的查询字符串中添加图书 ID,例如?book_id=123
  2. The Rails controller reads the ID as eg params[:book_id] Rails controller将 ID 读取为例如params[:book_id]
  3. The controller sets an instance variable , eg @book = Book.find(params[:book_id]) controller 设置了一个实例变量,eg @book = Book.find(params[:book_id])
  4. The Rails view sets the JS variable using .to_json as already shown in the question Rails视图使用.to_json设置 JS 变量,如问题中所示

This is just one application design, good for beginners.这只是一个应用程序设计,适合初学者。 Other designs include XHR (intermediate) or GraphQL (advanced).其他设计包括 XHR(中级)或 GraphQL(高级)。

暂无
暂无

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

相关问题 Javascript-如何将用于查询数据库的诺言将变量发送到前端? - Javascript - How do I send my variable to my frontend from the promise I use to query the database? 如何将画布附加到回形针上,以便将其保存到数据库中? - How do I get a canvas to attach to paperclip so I can save it into my database? 如何使用 javascript 从我的网站查询本地主机服务器? - How can I query localhost server from my website with javascript? 如何从表单中获取查询参数值 - How I can get a query parameter value from my form 如何从数据库中获取数据,以便可以通过JavaScript创建动态元素? - How do I fetch data from the database so I can create dynamic elements through javascript? 如何从Paypal取回数据,以便可以相应地更改MySQL数据库? - How do I get data back from Paypal so I can alter my MySQL database accordingly? 如何从本地存储中获取特定数据,以便在 JavaScript 中以模态显示它们? - How do I get specific data from my localstorage so I can show them in a modal in JavaScript? 如何查询JSON值并将其包含在javascript的rails助手中? - how do I query a JSON value and include it in a rails helper within javascript? 如何保存“图像点击”值,以便我可以继续在我的 PHP 程序中使用该值? - How can I save an "Image Click" value so that I can continue working with this value in my PHP program? 如何在Javascript中查询? - How do I query this in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM