如何访问视图中的嵌套属性?

时间:2018-02-21 18:03:03

标签: nested-forms

我正在构建我的第一个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

user.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#节目

  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

有人能指出我从嵌套表单中获取类别的方向,以显示在我的views / user / show页面上吗?我目前只获得奖项,没有嵌套表格的类别。提前谢谢!

1 个答案:

答案 0 :(得分:0)

您的accepts_nested_attributes_for中似乎有一个拒绝lamda,用于检查模式中不存在的类别中的属性。