2014-09-24 3 views
0

데이터베이스에 저장하도록 선택한 국가를 가져올 수없는 것 같습니다. 모델 데이터베이스에 country_code 문자열 열을 포함 시켰습니다.레일, 국가 보석 - 데이터베이스에 저장하지 않음

여기 부분을 찢는 해요 :

<%= f.label :country_code, "Country Traveled:" %> 
<%= f.country_select :country_code, prompt: "Select a country" %> 

내 컨트롤러에서 :

def new 
     @user = User.new 
    end 

def index 
    @user = User.all 
    end 

def create 
    @user = User.new(user_params) 

    if @user.save 
     redirect_to @user 
    else 
     render 'new' 
    end 
    end 

    def show 
    @user = User.find(params[:id]) 
    end 

    def edit 
    @user = User.find(params[:id]) 
    end 

private 
    def user_params 
     params.require(:user).permit(:country_code, :summary, :title, :text, :start_date, :end_date) 
    end 

내 model.rb에서 :

class User < ActiveRecord::Base 

    has_many :comments, dependent: :destroy 

    attr_accessor :country_code 

    validates :country_code, presence: true 

    validates :summary, presence: true,length: { minimum: 5, maximum: 100 } 

    validates :title, presence: true, 
         length: { minimum: 3 } 

    validates :text, presence: true, 
         length: { minimum: 5 } 

    validates :start_date, presence: true 

    validate :end_date_is_after_start_date 

private 
    def end_date_is_after_start_date 
    return if end_date.blank? || start_date.blank? 

     if end_date < start_date 
     errors.add(:end_date, "End Date cannot be before the Start Date") 
     end 
    end 

end 

뷰에서 드롭 다운을 잘 작동하지만 제출을 누르면 데이터베이스에 저장되지 않습니다. 내가 놓친 게 있니? 미리 감사드립니다!

+0

'binding.pry'를 사용하고 params를 입력하여': country_code'의 값을 확인할 수 있습니까? –

답변

0

전체 컨트롤러 및 모델을 공유하십시오.

일반적으로, 당신은 삭제할 수 있습니다

attr_accessor :country_code 

당신이 당신의 데이터베이스와 attr_accessor에 COUNTRY_CODE를 저장하기 때문에 쓸모 그냥 가상 속성, 당신은 여기에 대한 자세한 내용을보실 수 있습니다 : https://stackoverflow.com/a/20535041/3739191

PS를 : 가입 양식이 처음부터 시작되었거나 시작되지 않았습니까?

+0

Hi Spuyet - 답장을 보내 주셔서 감사합니다. 원래 질문을 전체 컨트롤러 및 모델로 업데이트했습니다. 정말로 당신이 도울 수 있기를 바란다. 당신의 도움을 주셔서 감사합니다! – gitastic

+0

감사합니다. 더 많은 정보가 필요합니다. 사용자가 저장 되었습니까? 그렇다면 country_code가 저장되지 않은 유일한 필드입니까? – Ayoros

+0

예 - 다른 모든 필드가 저장됩니다 (즉, 요약, 제목, 날짜 등). country_code가 이상한 이유로 데이터베이스에 저장하지 않습니다. =/ – gitastic

관련 문제