Ruby on rails 3 Rails:在另一个模型的创建表单中捕获模型id
我目前有三个模型,正在尝试将其中一个模型(webpage.id)的引用引入另一个模型(Micropost)-情况如下:Ruby on rails 3 Rails:在另一个模型的创建表单中捕获模型id,ruby-on-rails-3,models,nested-forms,relationships,Ruby On Rails 3,Models,Nested Forms,Relationships,我目前有三个模型,正在尝试将其中一个模型(webpage.id)的引用引入另一个模型(Micropost)-情况如下: class Member < ActiveRecord::Base has_many :microposts end class Webpage < ActiveRecord::Base has_many :microposts, :dependent => :destroy end class Micropost < ActiveRecor
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
我试图做的是从网页:show方法创建一个Micropost
这是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
class MicropostsController
从网页“显示”视图:
<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>
怎么了?
f、 对象%>
保存表单时,出现以下错误:
“找不到没有ID的网页”
当我从表单中删除对网页的引用时,表单成功地保存并设置了user\u id字段-因此这工作正常(但显然在这种情况下,网页的id为NULL)
你有没有想过我会错在哪里
干杯
Damo转储参数以确保网页id确实被传递 在您的视图中添加:
<%= debug params %>
转储参数以确保网页id确实被传递 在您的视图中添加:
<%= debug params %>