如何让index view retrieve/output数据在posting form中提交顺序排列?
How to make the index view retrieve/output data in the order submitted in the posting form?
使用嵌套表单,我创建了一个 post,它的顺序是 Text1
、Url1
、Text2
.
但是,当我检索此数据(控制器索引操作)并将其显示在视图 (index.html.erb
) 中时,它显示为 Text1
、Text2
、Url1
.我怎样才能让它以我在嵌套 posting 形式中给出的原始顺序显示在视图中?
你的帮助会很棒 - 我无法完成这项工作!
嵌套形式:
浏览器:
Post 型号
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
Url 型号
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"
Post控制器
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 %>
要补充 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
使用嵌套表单,我创建了一个 post,它的顺序是 Text1
、Url1
、Text2
.
但是,当我检索此数据(控制器索引操作)并将其显示在视图 (index.html.erb
) 中时,它显示为 Text1
、Text2
、Url1
.我怎样才能让它以我在嵌套 posting 形式中给出的原始顺序显示在视图中?
你的帮助会很棒 - 我无法完成这项工作!
嵌套形式:
浏览器:
Post 型号
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
Url 型号
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"
Post控制器
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 %>
要补充 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