Sinatra 和 Datamapper - 将数据插入一对多关系表

发布于 2024-09-10 01:41:25 字数 1155 浏览 1 评论 0原文

我有以下内容。每篇文章都有一个标题和正文以及最多三个 URL。我想将网址存储在不同的表中。因此,在我的表单中,我有一个 url 字段。然而它们不起作用,只有文章字段被输入到数据库中。我应该如何指定它们?有好心人可以帮我解决这个问题吗?

class Article
  include DataMapper::Resource

  property :id,     Serial
  property :title,  String
  property :body,   Text

  has n, :urls, through => Resource
end

class Url
  include DataMapper::Resource

  property :id,     Serial
  property :url_01,    String
  property :url_02,    String
  property :url_03,    String

  belongs_to :article
end

post '/create' do
  @article = Article.new(params[:article])
  if @article.save
    redirect "/articles"
  else
    redirect "/articles/new"
  end
end

--------------------------------------
<form action="/create" method="post">
  <p>
    <label>Article Title</label>
    <input type="text" name="article[title]">
  </p>
  <p>
    <label>Article Body</label>
    <input type="text" name="article[body]">
  </p>
  <p>
    <label>Url</label>
    <input type="text" name="article[url_01]">
  </p>

  <p>
    <input type="submit">
  </p>

I have the following. Each article has a title and a body and also up to three urls. I would want to store the urls in a different table. Therefore in my form, i've a field for the urls. However they are not working, only the article fields get entered into the database. how should i specify them? Could any kind soul help me out with this?

class Article
  include DataMapper::Resource

  property :id,     Serial
  property :title,  String
  property :body,   Text

  has n, :urls, through => Resource
end

class Url
  include DataMapper::Resource

  property :id,     Serial
  property :url_01,    String
  property :url_02,    String
  property :url_03,    String

  belongs_to :article
end

post '/create' do
  @article = Article.new(params[:article])
  if @article.save
    redirect "/articles"
  else
    redirect "/articles/new"
  end
end

--------------------------------------
<form action="/create" method="post">
  <p>
    <label>Article Title</label>
    <input type="text" name="article[title]">
  </p>
  <p>
    <label>Article Body</label>
    <input type="text" name="article[body]">
  </p>
  <p>
    <label>Url</label>
    <input type="text" name="article[url_01]">
  </p>

  <p>
    <input type="submit">
  </p>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

怪我鬧 2024-09-17 01:41:25

我相信

, through => Resource

只有在建立多对多关系时才需要这样做。我认为一对多是您想要的,但不需要这样做。查看关联页面上显示的帖子和评论关系。

编辑评论:

如果我是你,我会正常命名我的表单字段并手动构造数据库对象,例如:

<form action="/create" method="post">
  <p>
    <label>Article Title</label>
    <input type="text" name="title">
  </p>
  <p>
    <label>Article Body</label>
    <input type="text" name="body">
  </p>
  <p>
    <label>Url</label>
    <input type="text" name="url">
  </p>

  <p>
    <input type="submit">
  </p>

然后:

post '/create' do
  @article = Article.new(
      :title      => params[:title],
      :body       => params[:body]
  )
  @url = url.new(
      url_01 => params[:url]
  )
  @article.url = @url

  if @article.save
    redirect "/articles"
  else
    redirect "/articles/new"
  end
end

I believe that

, through => Resource

is only needed if you are doing a many-to-many relationship. A one-to-many, which I think is what you want, does not require that. Check out the post and comment relationship shown on the associations page.

EDIT for comment:

If I were you, I would name my form fields normally and construct the database object manually, for example:

<form action="/create" method="post">
  <p>
    <label>Article Title</label>
    <input type="text" name="title">
  </p>
  <p>
    <label>Article Body</label>
    <input type="text" name="body">
  </p>
  <p>
    <label>Url</label>
    <input type="text" name="url">
  </p>

  <p>
    <input type="submit">
  </p>

and then:

post '/create' do
  @article = Article.new(
      :title      => params[:title],
      :body       => params[:body]
  )
  @url = url.new(
      url_01 => params[:url]
  )
  @article.url = @url

  if @article.save
    redirect "/articles"
  else
    redirect "/articles/new"
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文