메시지 모델과 사용자 모델이 있습니다. 내 메시지 belongs_to
내 사용자와 사용자 has_many
메시지.컨트롤러에 ID 값 전달, 대량 할당 보안 오류
사용자가 공개 프로필 페이지 (쇼 템플릿)에있는 동안 다른 사용자에게 비공개 메시지를 보내도록 허용하려고합니다. 나는 많은 시도를했지만 궁극적으로는 ID가 attr_accessible
인 것을 요구하는 문제로 되돌아 간다. 내가 뭔가 잘못하고 있는거야?
내 메시지 모델은 :user_id
(현재 사용자, sending_from
ID), :to_id
, :content
입니다.
<%= form_for([current_user, @message]) do |f| %>
<%= f.hidden_field :to_id, :value => @user.id %>
<div class="field">
<%= f.text_area :content, placeholder: "Send a private message..." %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
는이 양식을 제출하면, 나는 간다,
def show
@user = User.find(params[:id])
@microposts = @user.microposts.paginate(page: params[:page])
if user_signed_in?
@message = current_user.messages.build(params[:messages], to_id: @user.id)
end
end
이 내 메시지에 난 항상 오류가,
def create
@message = current_user.messages.build(params[:message])
redirect_to user_path(params[:message][:to_id])
end
그러나 조치를 작성
`Can't mass-assign protected attributes: to_id`
:to_id attr_accessible
으로 해결할 수있는 것처럼 보이지만 그렇게하는 것이 안전하지 않다고 들었습니다. 내가 뭔가 잘못하고 있는거야? 이 문제로 저를 죽였습니다.
도움을 주시면 감사하겠습니다. 감사합니다
나는이 편집되기 전에, 당신은 몇 가지 [Alots] (http://hyperboleandahalf.blogspot.com 처리해야 할 것 : 그런 식으로 일을하는 이유는 다음과 같은 추가 화이트리스트 매개 변수를 추가 할 수 있습니다 /2010/04/alot-is-better-than-you-at-everything.html)! –