id nil has_many :through 但参数存在 :id

id nil has_many :through but in parameters exist :id

我有 3 个模型与 has_many :through 关系。用户、事件和画廊。在方法 newcreate in gallery_controller 中,我需要得到 event_id,但是我得到一个零 event_id。但是在 mozilla 控制台和参数中,存在 id。我不知道我做错了什么?

我也想知道 newcreate 动作的结构是否可以?我想在创建之前为事件添加一个画廊,同时在 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

图片错误

http://i.stack.imgur.com/mk1Ti.png

错误是因为您的 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 => [],除非您的表单有 userseventsnested fields 需要保存。

我想我发现了你的问题。对我来说似乎有更多的问题。但最初要解决您的问题:您需要添加 event_id 以允许参数方法:

  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 %>