2017-11-24 1 views
0

레일스 앱에 기존 기사를 업로드합니다. 내 요청에 created_at을 매개 변수로 보내고 있습니다 (this answer). 그러나이 속성은 "통과"하지 않는 것 같습니다. 나는 puts(params[:created_at]) 일 수 있고 내 사용자 정의 created_at을 볼 수 있지만 아직 로그에 기사는 현재 시간 소인 created_at과 함께 INSERT됩니다.모델 만들기가 맞춤 매개 변수를 재정의합니다. created_at

class ArticlesController < ApplicationController 
    before_action :set_article, only: [:show, :update, :destroy] 
    ... 
    # POST /articles 
    def create 
    @section = Section.friendly.find(params[:section_id]) 
    # Can't let people publish by default 
    @article = @section.articles.build(
     article_params.merge(is_published: false) 
    ) 

    if @article.save 
     render json: @article, status: :created, location: @article 
    else 
     render json: @article.errors, status: :unprocessable_entity 
    end 
end 
end 

내 요청은 다음과 같습니다 :

http POST :3000/articles title='example' section_id=1 content="<p>the section exists.</p>" slug="example" created_at="2017-06-109T17:57:55.149-05:00" 

로그 : 여기

내 기사 컨트롤러에서

Started POST "/articles" for 127.0.0.1 at 2017-11-24 12:05:06 -0500 
Processing by ArticlesController#create as HTML 
    Parameters: {"title"=>"example", "section_id"=>"1", "content"=>"<p>the section exists.</p>", "slug"=>"example", "created_at"=>"2017-06-109T17:57:55.149-05:00", "article"=>{"title"=>"example", "slug"=>"example", "content"=>"<p>the section exists.</p>", "created_at"=>"2017-06-109T17:57:55.149-05:00", "section_id"=>"1"}} 
    Section Load (0.3ms) SELECT "sections".* FROM "sections" WHERE "sections"."slug" = $1 ORDER BY "sections"."id" ASC LIMIT $2 [["slug", "1"], ["LIMIT", 1]] 
    Section Load (0.4ms) SELECT "sections".* FROM "sections" WHERE "sections"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] 
    (0.2ms) BEGIN 
    SQL (0.6ms) INSERT INTO "articles" ("title", "slug", "content", "is_published", "created_at", "updated_at", "section_id") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["title", "example"], ["slug", "example"], ["content", "<p>the section exists.</p>"], ["is_published", "f"], ["created_at", "2017-11-24 12:05:06.175751"], ["updated_at", "2017-11-24 12:05:06.175751"], ["section_id", 1]] 
    (0.8ms) COMMIT 
Completed 201 Created in 38ms (Views: 1.1ms | ActiveRecord: 8.0ms) 

schema.rb :

create_table "articles", force: :cascade do |t| 
    ... 
    t.datetime "created_at", null: false 

내 모델에는 요청을 망칠 여분의 메소드/콜백이 없습니다. 관계 만 포함합니다. 이것이 관련성이 있는지 나는 모르겠지만 GitHub 문제에서 어딘가에 그것을 보았다. 기사와 사용자는 저자가 많은 모델에 속하며 사용자를 인증하는 데 사용한다.

결론적으로 앱은 created_at 매개 변수를 수신하지만 기본 타임 스탬프로 다시 정의됩니다. record_timestamps을 false로 설정하면 created_atnil이됩니다.

created_at이 겉으로보기에 무시당하는 이유는 무엇입니까? 최대에 의해 주어진

레일 5.1, 루비 2.4.2, 포스트 그레스 10.1

+0

'2017-06-109T17 : 57 : 55.149-05 : 00'은 유효한 타임 스탬프가 아닙니다. 내가 아는 한 6 월에는 106 일이 없다. D – max

+0

오 세상에 ... 그게 문제였다. 이것이 수면 부족이하는 일입니다. 너무 바보 같아서 고마워. –

답변

1

답변 : 내 타임 스탬프에 syntatical 오류가 발생했습니다. 유효하지 않았고 Rails에서 무시되었습니다.

+0

조용히 실패하기 때문에 조금 까다 롭다. 원하는 경우 사용자 정의 설정기를 선언하고 'DateTime.parse'에 값을 전달하여 유효하지 않은 타임 스탬프 인 경우 예외를 발생시킬 수 있습니다 – max

관련 문제