Ruby on rails id nil有多个:到,但参数中存在:id
我有3个型号,有很多:通过关系。用户、事件和库。在Ruby on rails id nil有多个:到,但参数中存在:id,ruby-on-rails,ruby,has-many-through,Ruby On Rails,Ruby,Has Many Through,我有3个型号,有很多:通过关系。用户、事件和库。在gallery\u controller中的new和create方法中,我需要获取事件id,但是我得到了一个nil事件id。但是在mozilla控制台和参数中,存在id。我不知道我做错了什么 我还想知道new和create操作的结构是否正常?我想在创建之前为一个事件添加一个库,同时在当前用户库中,我无法通过前面的问题测试它 谢谢,干杯 class Event < ActiveRecord::Base has_many: galleries
gallery\u controller
中的new
和create
方法中,我需要获取事件id,但是我得到了一个nil事件id。但是在mozilla控制台和参数中,存在id。我不知道我做错了什么
我还想知道new
和create
操作的结构是否正常?我想在创建之前为一个事件添加一个库,同时在当前用户库中,我无法通过前面的问题测试它
谢谢,干杯
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
形成新画廊
<%= 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 %>
图像错误
错误是因为您的
新方法中有一个输入错误
这条线
@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
您的库参数也需要调整
def gallery_params
params.require(:gallery).permit(:description,:name)
end
你不想包括:event\u attributes=>[]
,:user\u attributes=>[]
,除非你的表单中有嵌套字段
用于用户
和需要保存的事件
。我想我发现了你的问题。对我来说,似乎还有更多的问题。但首先要解决您的问题:您需要添加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
然后在表单中,将外键字段添加为隐藏:
<%= f.hidden_field :event_id %>
<%= 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 %>