2010-07-18 3 views
0

다음과 같습니다. 각 기사에는 제목과 본문이 있으며 최대 세 개의 URL이 있습니다. 나는 다른 테이블에 URL을 저장하고 싶습니다. 따라서 내 양식에는 URL 필드가 있습니다. 그러나 그것들은 작동하지 않고 기사 필드 만 데이터베이스에 입력됩니다. 어떻게 지정해야합니까? 어떤 종류의 영혼이 나를 도와 줄 수 있습니까?Sinatra 및 Datamapper - 일대 다 관계 테이블에 데이터 삽입

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> 

답변

1

난 당신이 다 대다 관계를하고 있다면

, through => Resource 

만 필요하다고 생각합니다. 내가 생각하는 일대 다 (one-to-many)는 그것을 요구하지 않습니다. the associations page에 게시 된 게시물 및 댓글 관계를 확인하십시오. 코멘트

편집 : 다음

<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 
+0

그 모양은? 입력이 데이터베이스에 저장되지 않습니다. –

+0

위의 예에서 그는 html 형식의 필드로 "url"을 가지고 있습니다. 이 세 가지를 모두 표시하고 있다면 url.new 생성자를 HTML 양식의 세 가지로 모두 업데이트해야합니다. –