我正在尝试检索名为“gnib”的模型的 id,并将其以隐藏字段中的表单提交,以便用户在单击附加到每个 gnib 的提交按钮时有效地重新发布“gnib”。但是,当我单击按钮时,出现错误:
ActiveRecord::Recordnotfound in Sharescontroller#create
Couldn't find Gnib with id=
如果有人可以帮助我解决这个问题,我将不胜感激。
笔尖型号
attr_accessible :title, :image, :content
mount_uploader :image, ImagesUploader
belongs_to :user
validates :content, presence: true, length: { maximum: 80 }
validates :user_id, presence: true
default_scope order: 'gnibs.created_at DESC'
has_many :reverse_shares, foreign_key: "regnib_id", class_name: "Share", dependent: :destroy
has_many :regnibbers, through: :reverse_shares
在我的用户模型中,我有:
用户模型
has_many :gnibs, dependent: :destroy
has_many :shares, foreign_key: "regnibber_id", dependent: :destroy
has_many :regnibs, through: :shares
我使用了一个名为“Share”的连接模型来促进转发或“regnib”功能。
分享模型
attr_accessible :regnib_id
belongs_to :regnibber, class_name: "User"
belongs_to :regnib, class_name: "Gnib"
validates :regnibber_id, presence: true
validates :regnib_id, presence: true
在共享控制器中,我有:
def create
@gnib = Gnib.find(params[:share][:regnib_id])
current_user.regnib!(@gnib)
redirect_to @gnib
end
在gnib 控制器中,我有:
def create
@gnib = current_user.gnibs.build(params[:gnib])
end
在显示功能下的用户控制器中,我有:
@gnib = current_user.gnibs.build(params[:gnib]) if signed_in?
用于渲染gnibs的部分在用户展示页面中,如下所示:
<li>
<span class="Title"><%= gnib.title %></span>
<span class="Image"><%= image_tag gnib.image_url(:thumb) %></span>
<span class="content"><%= gnib.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(gnib.created_at) %> ago.
</span>
<%= render 'regnib_form' %>
</li>
而regnib形式的部分是:
<% unless current_user?(@user) %>
<div id="regnib_form">
<% if current_user.regnibbing?(@gnib) %>
<% else %>
<%= form_for(current_user.shares.build(regnib_id: @gnib.id)) do |f| %>
<div><%= f.hidden_field :regnib_id %></div>
<%= f.submit "Regnib", class: "btn btn-large btn-primary" %>
<% end %>
<h4> Regnibs <%= @gnib.regnibbers.count %></h4>
<% end %>
</div>
<% end %>