2012-05-10 3 views
0

tickets.js.coffee :아약스 포스트 HashWithIndifferentAccess 오류에

$.ajax '/comments/add', 
    type: 'POST', 
    dataType: 'html' 
    success: (data) -> 
    alert 'success' 

<%= form_for @comment, :url => user_ticket_message_comments_path(@user, @ticket, m), :html => { :class => "add-comment", :id => "add-comment-" + @ticket.id.to_s } do |f| %> 
    <%= f.label :body, "Add comment" %> 
    <%= f.text_area :body %> 
    <%= f.hidden_field :message_id, :value => m.id %> 
    <%= f.submit "Add comment" %> 
<% end %> 

댓글 테이블 :

id | message_id | body 
---------------------- 

CommentsController :

def create 
    @comment = params[:comment] 
    @comment.save 
end 

내 routes.rb :

resources :messages do 
    resources :comments 
end 

는이 오류를 얻을 :

undefined method `save' for {"body"=>"awef", "message_id"=>"15"}:ActiveSupport::HashWithIndifferentAccess 

코멘트 메시지에 속하고 메시지는 많은 의견이있다.

어디에서이 오류를 수정해야합니까?

답변

2

params[:comment]Comment 개체가 아니기 때문에 Hash이 아니므로 "저장"할 수 없습니다. 당신은 새로운 Comment 객체를 생성하고 각 속성을 지정해야, 다음 Comment 객체 저장 : 앱, config.active_record.whitelist_attributestrue으로 설정 될 수 방법 "새로운"에 따라

def create 
    @comment = Comment.new(params[:comment]) 
    @comment.save 
end 

을하는 경우 위의 보안 오류가 발생하며 속성을 올바르게 할당하는 방법에 대한 자세한 내용은 Rails Guides on Mass-Assignment을 읽어야합니다 (실제로 읽어야합니다).

+0

참고해 주셔서 감사합니다. 내가 그것을 간과했다라고 생각할 수 없다. –

관련 문제