Rails:在另一个模型的创建表单中捕获模型ID

发布于 2024-10-27 10:36:25 字数 1733 浏览 1 评论 0原文

我目前有三个模型,并试图将其中一个模型 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国际总奸 2024-11-03 10:36:25

转储参数以确保 page_id 确实被传递。

在您看来添加:

<%= debug params %>

Dump the params to make sure that webpage_id is truly being passed.

In your view add:

<%= debug params %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文