【问题标题】:How to make the index view retrieve/output data in the order submitted in the posting form?如何使索引视图按照提交表单中提交的顺序检索/输出数据?
【发布时间】:2016-02-16 11:26:01
【问题描述】:

使用嵌套表单,我创建了一个按Text1Url1Text2 排序的帖子。

但是,当我检索此数据(控制器索引操作)并将其显示在视图中 (index.html.erb) 时,它显示为Text1Text2Url1。我怎样才能让它以我在嵌套过帐表单中给出的原始顺序显示在视图中?

您的帮助将是惊人的 - 我就是无法完成这项工作!

嵌套形式:

浏览器:

后模型

  class Post < ActiveRecord::Base
   has_many :texts
   has_many :urls 
   accepts_nested_attributes_for :texts, :urls
  end

文本模型

  class Text < ActiveRecord::Base
   belongs_to :post 
  end

网址模型

  class Url < ActiveRecord::Base
   belongs_to :post
  end

架构

 create_table "posts", force: :cascade do |t|
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
 end

 create_table "texts", force: :cascade do |t|
   t.text     "textattr"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
   t.integer  "post_id"
 end

 add_index "texts", ["post_id"], name: "index_texts_on_post_id"

 create_table "urls", force: :cascade do |t|
   t.string   "urlattr"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
   t.integer  "post_id"
 end

 add_index "urls", ["post_id"], name: "index_urls_on_post_id"

帖子控制器

 class PostsController < ApplicationController

  def index
    @posts = Post.includes(:texts, :urls).all.order(created_at: :desc)
  end

  def show
   @post = Post.find(params[:id])
   @texts = @post.texts 
   @urls = @post.urls 
   @photos = @post.photos 
  end 

  def new
    @post = Post.new 
  end 

  def create
   @post = Post.new(post_params) 
    if @post.save 
     redirect_to @post
    else 
     render 'new'
    end 
  end


   private 
    def post_params 
        params.require(:post).permit(:texts_attributes => [:textattr], :urls_attributes => [:urlattr], :photos_attributes => [:image])
     end 
 end

查看(Index.html.erb)

<% @posts.each do |post| %> 

 <% post.texts.each do |text|%>
  <%= text.textattr %> <br> 
 <% end %>

 <% post.urls.each do |url|%>
  <%= url.urlattr %> <br> 
 <% end %> 

<% end %> 

【问题讨论】:

    标签: jquery ruby-on-rails forms ruby-on-rails-4 erb


    【解决方案1】:

    补充Pavan的答案,你的模型结构有缺陷。

    您正试图通过两个单独的模型提取组合数据。由于您希望它们在两种情况下都存在,我想说将它们放入一个模型中:

    #app/models/post.rb
    class Post < ActiveRecord::Base
      has_many :meta, class_name: "Meta"
      accepts_nested_attributes_for :meta
    end
    
    #app/models/meta.rb
    class Meta < ActiveRecord::Base
      #id | text | url | created_at | updated_at
      belongs_to :post
    end
    

    这样,你会得到:

    @posts.each do |post|
      post.meta.each do |meta|
        meta.try(:text)
        meta.try(:url)
      end
    end
    

    上述迁移将是:

     create_table "posts", force: :cascade do |t|
       t.datetime "created_at", null: false
       t.datetime "updated_at", null: false
     end
    
     create_table "metas", force: :cascade do |t|
       t.text     "text"
       t.string   "url"
       t.datetime "created_at", null: false
       t.datetime "updated_at", null: false
       t.integer  "post_id"
     end
    

    【讨论】:

    • 嗨,理查德 - 感谢您的反馈。不幸的是,这不适用于其他排列。比如在表单中发布Text1 Url1 Url2 Text2,结果浏览器显示Text1 Url1 Text2 Url2 :/
    • 嗨,Richard,我知道这不是传统的,但我就是无法理解这个问题。我已经发布了另一个问题并纠正了任何歧义。如果有任何机会你可以看看并帮助我,我真的很感激! stackoverflow.com/questions/35455261/…
    • 当然,让我帮你看看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多