2013-02-14 1 views
0

매우 유사한 문제를 해결하는 몇 가지 질문이 있지만 질문자의 상황에서 해결 방법이나 약간의 미묘한 차이가 있습니다.전자 메일 확인 용 보호 속성 할당

속성 confirmed이 보호되어 있으므로 할당 할 수 없습니다. 그것을 조사한 후에, 다음과 같은 해결책이 효과가있는 것처럼 보입니다. token (보호 됨)과 confirmed을 모두 볼 수 있지만 그 아래에서`confirmed '를 설정할 수는 없습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

if [email protected] && params[:token] == @user.token 
    flash[:success] = @user.token, @user.confirmed 
    @user.confirmed = true 
    @user.save 
    end 

감사합니다.

답변

1

내 테스트 응용 프로그램 중 하나를 사용하여 시나리오를 복제하고 난 사실에 (귀하의 경우 확인) 출판 설정할 수입니다.

마이그레이션

def up 
    create_table :posts do |t| 
     t.string :title 
     t.boolean :published, :default => false 
     t.string :token 
     t.timestamps 
end 

모델

class Post < ActiveRecord::Base 
    attr_protected :published 

    before_create :generate_token 

    def generate_token 
    self.token = Time.now().to_i 
    end 

end 

컨트롤러

class PostsController < ApplicationController 

    def publish 
    @post = Post.find_by_token(params[:token]) 
    if [email protected] && params[:token] == @post.token # The second condition is not necessary in this case. 
     @post.published = true 
     @post.save 
    end 
    end 
end 

이 (가)// 1360827718를 게시 타격 후 DB 기록 양식 콘솔

1.9.3-p0-perf :001 > p = Post.new({:title => "Protests against Violence"}) 
=> #<Post id: nil, title: "Protests against Violence", published: false, token: nil, created_at: nil, updated_at: nil> 
1.9.3-p0-perf :002 > p.save 
    (0.2ms) BEGIN 
    SQL (7.3ms) INSERT INTO "posts" ("created_at", "published", "title", "token", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", Thu, 14 Feb 2013 07:41:58 UTC +00:00], ["published", false], ["title", "Protests against Violence"], ["token", 1360827718], ["updated_at", Thu, 14 Feb 2013 07:41:58 UTC +00:00]] 
    (7.5ms) COMMIT 
=> true 
1.9.3-p0-perf :003 > p.reload 
    Post Load (1.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1 [["id", 2]] 
=> #<Post id: 2, title: "Protests against Violence", published: false, token: "1360827718", created_at: "2013-02-14 07:41:58", updated_at: "2013-02-14 07:41:58"> 

만들기 - 여기에 콘솔

#<Post id: 2, title: "Protests against Violence", published: true, token: "1360827718", created_at: "2013-02-14 07:41:58", updated_at: "2013-02-14 07:45:33"> 

내가 PostgreSQL은이 코드를 실행하고의 출력은 3.2.11 레일과 ruby 1.9.3 및 컨트롤러의 게시 작업은 published = true를 설정합니다.

때로는 문제를 식별하는 데 도움이되지 않는 코드와이 코드의 답을 의도 한 코드를 비교하는 경우가 있습니다.

호프를 사용하면 문제를 쉽게 파악할 수 있습니다.

0

모델에서이 속성을 attr_accessible에 추가하십시오.

그리고 자세한 내용은이 읽기 : http://api.rubyonrails.org/classes/ActiveModel/MassAssignmentSecurity/ClassMethods.html

+0

나는 그런 식으로 접근 할 수있는 '확인'을 원하지 않습니다. 나는 그것을 보호 된 속성으로하고 싶습니다. – mattmattmatt

+0

그렇지 않으면 데이터베이스를 직접 요청하지 않는 이상 변경할 수 없습니다 – Phobos98