2013-06-27 2 views
0

레일스를 처음 접했을 때, 내가 잘못 될 가능성이있는 곳을보기 위해 내 코드를 점검 할 수 있습니까? Song 개체를 만들려고합니다.레일즈 - '새'폼에서 생성되지 않는 객체

내 SQLite 파일을 보면 새 노래를 만들 수 없음을 알 수 있습니다. 전경/노래에서

/new.html.erb

<!DOCTYPE html> 
<html> 
    <head> 
     <title>MusicDiscoveryApp</title> 
     <%= stylesheet_link_tag "application", :media => "all" %> 
     <%= javascript_include_tag "application" %> 
     <%= csrf_meta_tags %> 
    </head> 
    <body> 
      <%= form_for(@song, :html => {:id => 'add-to-library', :method => :post}) do |f| %> 
      <div class="item-info"> 
       <%= f.select :category_id, [['Pop', 1], ['Rock', 2], ['Rap', 3], ['Reggae', 4], ['Other', 5]] , {:prompt=>"Select A Category"}, :id=>"choose-category"%> 
      </div> 
       <div class="item-info"> 
       <%= f.text_field :name , :placeholder=>"Song Name"%> 
      </div> 
      <div class="item-info"> 
       <%= f.text_field :price, :placeholder=>"Price"%> 
      </div> 
       <div class="item-info"> 
       <%= f.text_area :details ,:placeholder=>"Details"%> 
      </div> 
      <div class="item-actions"> 
       <%= f.submit "Post to Songs Library", :class=>"add-song"%> 
      </div> 
      <% end %> 
    </body> 
</html> 

songs_controller.rb에서 '새로운'방법 - 자동

def new 
    @song = Song.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @song } 
    end 
    end 

songs_controller에서 '생성'방법

생성 하였다. rb - 자동 생성됨

def create 
    @song = Song.new(params[:song]) 
    end 

시간 내 주셔서 감사합니다. 나는 여기서 잘못 했어. 다시 한번 감사합니다.

+0

'데이터베이스에 새 레코드를 저장하지 않습니다 ActiveRecord'의'new' 방법. –

+0

@AlexWayne 그냥 호기심, 왜 안돼? 객체를 저장하지 않고 새 인스턴스를 갖는 유스 케이스가 있습니까? – GangstaGraham

+0

객체를 저장하기 전에 메모리에 객체를 만들고 싶기 때문에 객체를 조작 할 수있는 기회를 주며 유효성 검사를 통과하지 못할 수도 있습니다. 폼의 객체가 속성을 렌더링하게 만드는'new '컨트롤러 액션을 살펴보십시오. 아직 데이터가 없기 때문에 저장하고 싶지 않습니다. –

답변

2

노래가 저장되지 않습니다. 추가 : 컨트롤러에

@song = Song.new(params[:song]) 
@song.save 

이 방법을 만들거나 만들 새로운 변경

@song = Song.create(params[:song]) 
관련 문제