当数据结构很大时,将参数从视图传递给控制器 失败

问题描述 投票:0回答:1
Rails 5.2

在我的config / routes.rb中,我有:

post 'books_author', to: 'books#author'

当我运行耙道时,我得到:

books_author POST /books_author(.:format) books#author

在我的app / controllers / books_controller.rb中,我有:

def author
  books_collection = params[:books_collection]
  author_notes = params[:author_notes]
  render json: author_notes["2019"][author_notes]['main_author']
end

而且我认为,我有以下内容:

td
  = link_to 'Author Details', books_author_path(books_collection: books_collection, author_notes: author_notes), method: :post

author_notes数据结构非常大。

[运行应用程序,然后单击链接时,出现以下错误消息:

This site can’t be reached
The connection was reset

我在浏览器中看到了这一点,但是development.log文件中没有任何条目。

如果双击顶部的浏览器栏将其选中,然后将其复制并粘贴到文本编辑器中,则会看到整个请求(很大的请求)。

我尝试传递一个较小的数据结构,并且效果很好。有什么想法如何将更大的数据结构从视图传递到控制器方法?

ruby-on-rails
1个回答
0
投票

您需要在author操作中进行一些更改,使用实例变量,以便可以在视图中对其进行访问

def author
  @books_collection = params[:books_collection]
  @author_notes = params[:author_notes]
  render json: @author_notes["2019"][author_notes]['main_author']
end

在视图中

td
  = link_to 'Author Details', books_author_path(books_collection: @books_collection, author_notes: @author_notes), method: :post

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.