我有3个模型与has_many:通过关系。用户,活动和图库。在new
中的create
和gallery_controller
方法中,我需要获取event_id,但是我得到一个nil event_id。但是在mozilla控制台和参数中,存在id。我不是我做错了什么?
我还想知道new
和create
操作的结构是否合适?我想在创建之前为事件添加一个库,同时在current_user库中添加一个库,我还没有可以通过上一个问题测试它。
谢谢和欢呼。
class Event < ActiveRecord::Base
has_many: galleries
has_many: users, through: : galleries, : source => : users, : dependent => : destroy
accepts_nested_attributes_for: users
accepts_nested_attributes_for: galleries
end
class User < ActiveRecord::Base
has_many :galleries
has_many :events, through: :galleries, :dependent => :destroy
accepts_nested_attributes_for :events
accepts_nested_attributes_for :galleriesenter code here
end
class Gallery < ActiveRecord::Base
has_many :pictures, :dependent => :destroy
belongs_to :event
belongs_to :user
end
Gallery_controller
def new
@event = Event.find(params[:event_id])
@galery = Gallery.new
respond_to do |format |
format.html# new.html.erb
format.json {
render json: @gallery
}
end
end
def create
@event = Event.find(params[:id])
@gallery = @event.galleries.build(params[:gallery])
@gallery.user = current_user
respond_to do |format |
if@ gallery.save
if params[: images]# The magic is here;)
params[: images].each { | image | @gallery.pictures.create(image: image)
}
end
def gallery_params
params.require(:gallery).permit(:description,
:name,
:pictures,
:event_attributes => [],
:user_attributes => [],
)
end
form_新图库
<%= form_for [@event,@gallery], :html => { :class => 'form-horizontal', multipart: true } do |f| %>
<div class="control-group">
<%= f.label :name, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :name, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :description, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :description, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :pictures, :class => 'control-label' %>
<div class="controls">
<%= file_field_tag "images[]", type: :file, multiple: true %>
</div>
</div>
<div class="form-actions">
<%= f.submit :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
galleries_path, :class => 'btn btn-mini' %>
</div>
<% end %>
路由
resources :events do
resources :galleries
end
图片错误
答案 0 :(得分:1)
错误是因为您的new
方法中存在拼写错误。
这一行
@galery = Gallery.new
应该是
@gallery = Gallery.new
此外,您的create
方法存在一些需要修复的错误。
def create
@event = Event.find(params[:event_id])
@gallery = @event.galleries.build(gallery_params)
@gallery.user = current_user
respond_to do |format|
if @gallery.save
if params[:images]
params[:images].each { |image| @gallery.pictures.create(image: image)}
end
format.html { redirect_to @gallery, notice: 'Gallery was successfully created.' }
format.json { render json: @gallery, status: :created, location: @gallery }
else
format.html { render :new }
format.json { render json: @gallery.errors, status: :unprocessable_entity }
end
end
end
还有gallery_params
需要调整
def gallery_params
params.require(:gallery).permit(:description,:name)
end
您不希望包含:event_attributes => []
,:user_attributes => []
,除非您的表单nested fields
users
和events
需要保存。
答案 1 :(得分:0)
我想我发现了你的问题。对我来说似乎有更多问题。但最初要解决您的问题:您需要添加event_id以允许params方法:
params.require(:gallery).permit(:description,
:name,
:pictures,
:event_id, # this line should be here if your foreign key is event_id for gallery model.
:event_attributes => [],
:user_attributes => [],
)
此外,您的表单并不满足正确的实例变量。差异在这里:
<%= form_for [@event,@gallery], :html => { :class => 'form-horizontal', multipart: t # you wrote @gallery here but in your controller you wrote:
# controller's action:
@galery = Gallery.new
建议的方法是隐藏外键以及从事件构建:
@event = Event.find(params[:event_id])
@gallery = @event.gallaries.build #note: 1.spelling and 2. building from @event object
然后在表单中添加外键字段为隐藏:
<%= f.hidden_field :event_id %>