【发布时间】:2018-02-21 18:03:03
【问题描述】:
我正在构建我的第一个 Rails 应用程序,并努力让我的类别嵌套属性出现在我的用户/显示视图中。
观看次数/用户/节目
<%= render 'layouts/header' %>
<h1>Your Cars:</h1>
<ul>
<% if user_signed_in? %>
<% current_user.cars.each do |car| %>
<h2> <%= "#{car.make} #{car.model}"%></h2>
<h2> <%= car.year%></h2>
<h2> <%= car.color %></h2>
<% if car.awards.any? %>
<h2>Awards:</h2>
<% end %>
<% car.awards.each do |award| %>
<li><%= award.title %></li>
<li><%= award.year %></li>
<li><%= award.description %></li>
<% award.categories.each do |category| %>
<li><%= category.name %></li>
<%end%>
<li><button class="btn btn-default"><%= link_to 'Delete Award', award, :data => {:confirm => "You Sure?", :method => :delete} %></button></li><br>
<%end%>
<button class="btn btn-default"><%= link_to 'Edit', edit_car_path(car) %></button>
<button class="btn btn-default"><%= link_to 'Delete', car, :data => {:confirm => "You Sure?", :method => :delete} %></button>
<button class="btn btn-default"><%= link_to 'Add Award', new_car_award_path(car) %></button>
<%end%>
<%end%>
</ul>
<% if user_signed_in? %>
<button class="btn btn-default"><%=link_to "Add a car", new_user_car_path(current_user)%></button>
<%end%>
category.rb
class Category < ApplicationRecord
belongs_to :award
end
award.rb
class Award < ApplicationRecord
belongs_to :car
has_one :user, through: :cars
has_many :categories
validates :year, length: {is: 4}
accepts_nested_attributes_for :categories, allow_destroy: true, reject_if: lambda {|attributes| attributes['kind'].blank?}
end
用户.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:facebook]
has_many :cars
has_many :awards, through: :cars
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
end
end
end
观看次数/奖项/表格
<%= form_for [@car, @award] do |f| %>
<%= f.label :title %>
<%= f.text_field :title %><br>
<%= f.label :year %>
<%= f.text_field :year %><br>
<%= f.label :description %>
<%= f.text_area :description %><br>
Categories:
<ul>
<%= f.fields_for :categories do |categories_form| %>
<li>
<%= categories_form.label :name %>
<%= categories_form.text_field :name %>
<%= categories_form.check_box :_destroy%>
</li>
<% end %>
</ul>
<%= f.submit %>
<%end%>
users_controller#show
def show
@user = User.find_by(id: params[:user_id])
end
架构
ActiveRecord::Schema.define(version: 20180221183858) do
create_table "awards", force: :cascade do |t|
t.string "title"
t.integer "car_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "description"
end
create_table "cars", force: :cascade do |t|
t.string "make"
t.string "model"
t.integer "year"
t.string "color"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "categories", force: :cascade do |t|
t.string "name"
t.integer "award_id"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "provider"
t.string "uid"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["provider"], name: "index_users_on_provider"
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["uid"], name: "index_users_on_uid"
end
end
谁能指出我从嵌套表单中获取类别以显示在我的视图/用户/显示页面上的方向?我目前只获得奖项,嵌套表格中没有任何类别。提前致谢!
【问题讨论】:
-
你能添加你的
users_controller#show方法吗? -
已添加到底部,感谢查看!
-
您的数据库中的类别表中是否有
award_id(查看您的 schema.rb) -
顺便说一句。您不必使用
find_by- 默认情况下它是 Rails 正在寻找的 id,因此您可以写:User.find(params[:user_id]) -
也许只是添加你的 schema.rb 文件
标签: nested-forms