在rails中显示表单数据的最佳方法是什么?

时间:2022-09-15 20:51:10

I am very new to Rails.

我是Rails的新手。

I am looking for the most common and 'rails-like' way to display the output of form data. I know that Simple form and Formtastic are good options to write concise code and display input fields in a user friendly manner. However, I am looking for something slightly different, I want to display the data from these forms as output, and I want to do it in a similar and concise manner. I haven't been able to find anything the last few days that I have been searching around so I figured I should ask Stack Overflow.

我正在寻找最常见的“轨道式”方式来显示表单数据的输出。我知道Simple form和Formtastic是以用户友好的方式编写简洁代码和显示输入字段的好选择。但是,我正在寻找稍微不同的东西,我想从这些表单中显示数据作为输出,我想以类似和简洁的方式进行。过去几天我一直在找不到任何东西,所以我想我应该问Stack Overflow。

What do you normally do:

你通常做什么:

  • Write custom helpers to be able to write concise code and display the output data?
  • 编写自定义帮助程序,以便能够编写简洁的代码并显示输出数据?

  • Use Simpleform/Formtastic, disable the fields, and modify the css to make it look the way you want?
  • 使用Simpleform / Formtastic,禁用字段,并修改CSS以使其看起来像你想要的那样?

  • Is there a common gem that does this? Or perhaps another way I haven't thought about.
  • 有没有共同的宝石这样做?或者也许是我没想过的另一种方式。

    First, thanks for the initial responses. I thought I should clarify my question. Since the answers don't quite hit the mark. I already have a show method in the controller, as well as a view template for it. Currently I am displaying each field individually in the form for about 12 fields using form_for. See example snippet below:

    首先,感谢最初的回应。我想我应该澄清一下我的问题。由于答案不是很明显。我已经在控制器中有一个show方法,以及它的视图模板。目前,我使用form_for在表单中单独显示每个字段大约12个字段。请参阅下面的示例代码段:

      <%= form_for(@event) do |f| %>
         <div class="row-fluid">
          <div class="span6">
           <%= render 'shared/error_messages' %>
    
             <%= f.label :title %>
             <%= f.text_field :title, :disabled => true %>
    
             <%= f.label :start_date %>
             <%= f.date_field :start_date, :disabled => true %>
          </div>
         </div>
       <% end %>
    

    I guess maybe a better question would be, is their an equivalent to form_for method that display data for users, not to edit it but just to read it? It seems like their would be a a standard way to do it that I haven't discovered.

    我想也许更好的问题是,它们是否相当于form_for方法,它为用户显示数据,而不是编辑它而只是为了阅读它?看起来他们将是一个我没有发现的标准方法。

    2 个解决方案

    #1


    0  

    The easiest way to build forms is to use rails g scaffold Example name:string This would generate the Model, Views, Controller, and the necessary database migrations for a Model named Example with a Name attribute that is a string.

    构建表单的最简单方法是使用rails g scaffold示例名称:string这将为名为Example的Model生成Model,Views,Controller和必要的数据库迁移,并使用Name属性作为字符串。

    You would then use html and css to style the view how you want.

    然后,您将使用html和css为视图设置样式。

    There is a ton of useful info for you on the rails guides here.

    这里有关于导轨指南的大量有用信息。

    #2


    0  

    Although this question is rather ambiguous, you must appreciate that this functionality is exactly what Rails is built for (submit data & be able to display / manipulate it somewhere else)

    虽然这个问题很模糊,但你必须明白这个功能正是Rails的构建(提交数据并能够在其他地方显示/操作它)


    Data

    The data in an MVC application is bound by one very important factor - a database

    MVC应用程序中的数据受一个非常重要的因素 - 数据库的约束

    If you're submitting data through a form, your goal is to store it in your database, and display it in other views / controller methods

    如果您通过表单提交数据,那么您的目标是将其存储在数据库中,并将其显示在其他视图/控制器方法中

    Therefore, the blunt question to your answer is to abide by MVC processes, and save your data to a central repository (database or other), which you can call later:

    因此,对您的回答的直接问题是遵守MVC流程,并将您的数据保存到*存储库(数据库或其他),您可以稍后调用它们:

    在rails中显示表单数据的最佳方法是什么?


    View

    To get your data into your data store, you first need to submit it

    要将数据导入数据存储,首先需要提交数据

    The view part of MVC is where you can display your UI, and consequently is where you can put your form. There is a whole tutorial about how to design this here, but for demonstrations' sake, here's an example of how you'd use your form:

    MVC的视图部分是您可以显示UI的位置,因此您可以在其中放置表单。这里有一个关于如何设计它的完整教程,但为了演示,这里是一个如何使用表单的示例:

    #app/views/posts/new.html.erb
    <%= form_for @post do |f| %>
        <%= f.text_field :title %>
        <%= f.text_field :body %>
        <%= f.submit %>
    <% end %>
    

    Controller

    The data you input into your view will be sent to your controller on save

    输入到视图中的数据将在保存时发送到您的控制器

    The data is sent via the HTML POST method to be read by Rails as a params[] hash. This hash contains all the sent items from your HTML form, and can be used like this:

    数据通过HTML POST方法发送,由Rails作为params []哈希读取。此哈希包含HTML表单中的所有已发送项目,可以像这样使用:

    #app/controllers/posts_controller.rb
    def new
        @post = Post.new
    end 
    
    def create 
        @post = Post.new(post_params)
        @post.save
    end
    
    private
    def post_params
        params.require(:post).permit(:title, :body)
    end
    

    Model

    The .save method on your newly created Post object, basically tells your model to put the data into your database (or other data store), which you can then pull later (using the .find method or similar)

    新创建的Post对象上的.save方法,基本上告诉您的模型将数据放入您的数据库(或其他数据存储),然后您可以稍后提取(使用.find方法或类似方法)

    #app/models/post.rb
    Class Post < ActiveRecord::Base
       #stuff here
    end
    

    It's important to note your models don't store your data, they simply provide an interface (API if you will) to save the data into some sort of data store (namely, a DB)

    重要的是要注意您的模型不存储您的数据,它们只是提供一个接口(如果您愿意,API)将数据保存到某种数据存储(即数据库)

    Models are super important because they allow you to structure & relate your data, creating the ability to deploy really deep & powerful applications

    模型非常重要,因为它们允许您构建和关联数据,从而创建部署真正深入和强大的应用程序的能力

    #1


    0  

    The easiest way to build forms is to use rails g scaffold Example name:string This would generate the Model, Views, Controller, and the necessary database migrations for a Model named Example with a Name attribute that is a string.

    构建表单的最简单方法是使用rails g scaffold示例名称:string这将为名为Example的Model生成Model,Views,Controller和必要的数据库迁移,并使用Name属性作为字符串。

    You would then use html and css to style the view how you want.

    然后,您将使用html和css为视图设置样式。

    There is a ton of useful info for you on the rails guides here.

    这里有关于导轨指南的大量有用信息。

    #2


    0  

    Although this question is rather ambiguous, you must appreciate that this functionality is exactly what Rails is built for (submit data & be able to display / manipulate it somewhere else)

    虽然这个问题很模糊,但你必须明白这个功能正是Rails的构建(提交数据并能够在其他地方显示/操作它)


    Data

    The data in an MVC application is bound by one very important factor - a database

    MVC应用程序中的数据受一个非常重要的因素 - 数据库的约束

    If you're submitting data through a form, your goal is to store it in your database, and display it in other views / controller methods

    如果您通过表单提交数据,那么您的目标是将其存储在数据库中,并将其显示在其他视图/控制器方法中

    Therefore, the blunt question to your answer is to abide by MVC processes, and save your data to a central repository (database or other), which you can call later:

    因此,对您的回答的直接问题是遵守MVC流程,并将您的数据保存到*存储库(数据库或其他),您可以稍后调用它们:

    在rails中显示表单数据的最佳方法是什么?


    View

    To get your data into your data store, you first need to submit it

    要将数据导入数据存储,首先需要提交数据

    The view part of MVC is where you can display your UI, and consequently is where you can put your form. There is a whole tutorial about how to design this here, but for demonstrations' sake, here's an example of how you'd use your form:

    MVC的视图部分是您可以显示UI的位置,因此您可以在其中放置表单。这里有一个关于如何设计它的完整教程,但为了演示,这里是一个如何使用表单的示例:

    #app/views/posts/new.html.erb
    <%= form_for @post do |f| %>
        <%= f.text_field :title %>
        <%= f.text_field :body %>
        <%= f.submit %>
    <% end %>
    

    Controller

    The data you input into your view will be sent to your controller on save

    输入到视图中的数据将在保存时发送到您的控制器

    The data is sent via the HTML POST method to be read by Rails as a params[] hash. This hash contains all the sent items from your HTML form, and can be used like this:

    数据通过HTML POST方法发送,由Rails作为params []哈希读取。此哈希包含HTML表单中的所有已发送项目,可以像这样使用:

    #app/controllers/posts_controller.rb
    def new
        @post = Post.new
    end 
    
    def create 
        @post = Post.new(post_params)
        @post.save
    end
    
    private
    def post_params
        params.require(:post).permit(:title, :body)
    end
    

    Model

    The .save method on your newly created Post object, basically tells your model to put the data into your database (or other data store), which you can then pull later (using the .find method or similar)

    新创建的Post对象上的.save方法,基本上告诉您的模型将数据放入您的数据库(或其他数据存储),然后您可以稍后提取(使用.find方法或类似方法)

    #app/models/post.rb
    Class Post < ActiveRecord::Base
       #stuff here
    end
    

    It's important to note your models don't store your data, they simply provide an interface (API if you will) to save the data into some sort of data store (namely, a DB)

    重要的是要注意您的模型不存储您的数据,它们只是提供一个接口(如果您愿意,API)将数据保存到某种数据存储(即数据库)

    Models are super important because they allow you to structure & relate your data, creating the ability to deploy really deep & powerful applications

    模型非常重要,因为它们允许您构建和关联数据,从而创建部署真正深入和强大的应用程序的能力