Oss*_*sie 7 ruby-on-rails fields-for ruby-on-rails-4
我之前发布了一个关于此的问题,并被建议阅读大量相关信息.我已阅读并尝试实施约30种不同的解决方案.这些都不适合我.
这就是我所拥有的.
我有一个Miniatures模型.我有制造商型号.Miniatures有许多制造商通过Productions模型.
这些关联似乎设置正确,因为我可以在我的视图中显示它们并通过控制台创建它们.我遇到问题的方法是让Miniatures NEW和EDIT视图创建并更新到Productions表.
在控制台中的命令 @miniature.productions.create(manufacturer_id: 1)有效,这使我相信我应该能够在表单中执行相同的操作.
我认为我的问题总是在Miniatures Controller中,特别是CREATE函数.我在那里尝试了很多其他人的解决方案,没有人能做到这一点.我的field_for形式的东西也可能是错误的,但这似乎不那么繁琐.
我已经坚持了几天,虽然还有其他我可以继续工作的事情,如果这种关联不可能,那么我需要重新考虑我的整个应用程序.
表单现在在Productions表中创建一行,但不包括所有重要的manufacturer_id.
任何帮助非常感谢.
我的新微缩形式
<% provide(:title, 'Add miniature') %>
<h1>Add a miniature</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@miniature) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.fields_for :production do |production_fields| %>
<%= production_fields.label :manufacturer_id, "Manufacturer" %>
<%= production_fields.select :manufacturer_id, options_from_collection_for_select(Manufacturer.all, :id, :name) %>
<% end %>
<%= f.label :release_date %>
<%= f.date_select :release_date, :start_year => Date.current.year, :end_year => 1970, :include_blank => true %>
<%= f.submit "Add miniature", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
微型控制器
class MiniaturesController < ApplicationController
before_action :signed_in_user, only: [:new, :create, :edit, :update]
before_action :admin_user, only: :destroy
def productions
@production = @miniature.productions
end
def show
@miniature = Miniature.find(params[:id])
end
def new
@miniature = Miniature.new
end
def edit
@miniature = Miniature.find(params[:id])
end
def update
@miniature = Miniature.find(params[:id])
if @miniature.update_attributes(miniature_params)
flash[:success] = "Miniature updated"
redirect_to @miniature
else
render 'edit'
end
end
def index
@miniatures = Miniature.paginate(page: params[:page])
end
def create
@miniature = Miniature.new(miniature_params)
if @miniature.save
@production = @miniature.productions.create
redirect_to @miniature
else
render 'new'
end
end
def destroy
Miniature.find(params[:id]).destroy
flash[:success] = "Miniature destroyed."
redirect_to miniatures_url
end
private
def miniature_params
params.require(:miniature).permit(:name, :release_date, :material, :scale, :production, :production_attributes)
end
def admin_user
redirect_to(root_url) unless current_user.admin?
end
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
end
Run Code Online (Sandbox Code Playgroud)
微型模型
class Miniature < ActiveRecord::Base
has_many :productions, dependent: :destroy
has_many :manufacturers, :through => :productions
accepts_nested_attributes_for :productions
validates :name, presence: true, length: { maximum: 50 }
validates :material, presence: true
validates :scale, presence: true
validates_date :release_date, :allow_blank => true
def name=(s)
super s.titleize
end
end
Run Code Online (Sandbox Code Playgroud)
生产模式
class Production < ActiveRecord::Base
belongs_to :miniature
belongs_to :manufacturer
end
Run Code Online (Sandbox Code Playgroud)
制造商型号
class Manufacturer < ActiveRecord::Base
has_many :productions
has_many :miniatures, :through => :productions
validates :name, presence: true, length: { maximum: 50 }
accepts_nested_attributes_for :productions
end
Run Code Online (Sandbox Code Playgroud)
Hel*_*rra 10
而不是打电话:
@production = @miniature.productions.create
Run Code Online (Sandbox Code Playgroud)
尝试Rails的"构建"方法:
def new
@miniature = Miniature.new(miniature_params)
@miniature.productions.build
end
def create
@miniature = Miniature.new(miniature_params)
if @miniature.save
redirect_to @miniature
else
render 'new'
end
end
Run Code Online (Sandbox Code Playgroud)
使用构建方法使用ActiveRecord的自动保存关联功能.
见http://api.rubyonrails.org/classes/ActiveRecord/AutosaveAssociation.html
您还需要更新params方法,例如
def miniature_params
params.require(:miniature).permit(:name, :release_date, :material, :scale, productions_attributes: [:manufacturer_id])
end
Run Code Online (Sandbox Code Playgroud)
你的fields_for应该是复数(我认为)......
<%= f.fields_for :productions do |production_fields| %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11039 次 |
| 最近记录: |