Rails:在另一个模型的创建表单中捕获模型ID
我目前有三个模型,并试图将其中一个模型 (webpage.id) 的引用引入另一个模型 (Micropost) - 情况如下:
class Member < ActiveRecord::Base
has_many :microposts
end
class Webpage < ActiveRecord::Base
has_many :microposts, :dependent => :destroy
end
class Micropost < ActiveRecord::Base
belongs_to :member
belongs_to :webpage
end
我想做的是从网页创建一个微帖子:显示'方法。
这是 Micropost 控制器:
class MicropostsController < ApplicationController
def create
@micropost = current_member.microposts.build(params[:micropost])
@webpage = Webpage.find(params['webpage_id'])
@micropost.webpage = @webpage
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_path
else
@feed_items = []
render 'pages/home'
end
end
end
从网页“显示”视图:
<table class="front" summary="For signed-in members">
<tr>
<td class="main">
<h1 class="micropost">What's up?</h1>
<%= form_for @micropost do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.text_area :content %>
<input type="hidden" id="webpage_id" name="micropost[webpage_id]" value="<%= @webpage.id %>"/>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
</td>
</tr>
</table>
当我保存表单时,这是我收到的错误:
“无法找到没有 ID 的网页”
当我从表单中删除对网页的引用时,表单成功保存并设置了 user_id 字段 - 所以这工作正常(但显然在这种情况下,webpage_id 为 NULL)。
对我哪里出错有什么想法吗?
加油
达摩
I currently have three models and am trying to bring in a reference of one of the models (webpage.id) into another model (Micropost) - here is the situation:
class Member < ActiveRecord::Base
has_many :microposts
end
class Webpage < ActiveRecord::Base
has_many :microposts, :dependent => :destroy
end
class Micropost < ActiveRecord::Base
belongs_to :member
belongs_to :webpage
end
What I am trying to do is create a Micropost from the Webpage ':show' method.
Here is the Micropost controller:
class MicropostsController < ApplicationController
def create
@micropost = current_member.microposts.build(params[:micropost])
@webpage = Webpage.find(params['webpage_id'])
@micropost.webpage = @webpage
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_path
else
@feed_items = []
render 'pages/home'
end
end
end
and from the Webpage 'show' view:
<table class="front" summary="For signed-in members">
<tr>
<td class="main">
<h1 class="micropost">What's up?</h1>
<%= form_for @micropost do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.text_area :content %>
<input type="hidden" id="webpage_id" name="micropost[webpage_id]" value="<%= @webpage.id %>"/>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
</td>
</tr>
</table>
When I save the form, this is the error I am getting:
"Couldn't find Webpage without an ID"
When I remove reference to Webpage from the form, the form successfully saves and sets the user_id field - so this is working fine (but obviously in this case the webpage_id is NULL).
Any thoughts on where I am going wrong?
Cheers,
Damo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
转储参数以确保 page_id 确实被传递。
在您看来添加:
Dump the params to make sure that webpage_id is truly being passed.
In your view add: