0
사용자가 기사를 팔로우하거나 언 폴링 할 수있는 앱을 만들려고합니다. 이를 위해 Customer
, Article
및 Pin
의 세 가지 모델을 만들었습니다. 기사를 팔로우하거나 언 폴트하기
Customer
has_many articles
has_many pins
Article
has_many pins
belongs_to customer
Pins
belongs_to customer
belongs_to article
가 나는
Pin
가
Article
중첩해야 믿습니다
이
는 관계입니다. 이 같은 내route.rb
보기 :
article#index
에서
resources :articles do
resources :pins, :only => [:create, :destroy]
end
end
나는 관계를 만들거나 파괴 형태가 : 여기
# To create
<%= form_for [article, current_customer.pins.new] do |f| %>
<%= f.submit "Pin" %>
<% end %>
# To destroy which doesn't work because i guess you can't do the form like that
<%= form_for [article, current_customer.pins.destroy] do |f| %>
<%= f.submit "Pin" %>
<% end %>
을 해당 컨트롤러 액션은 다음과 같습니다
def create
@article = Article.find(params[:article_id])
@pin = @article.pins.build(params[:pin])
@pin.customer = current_customer
respond_to do |format|
if @pin.save
format.html { redirect_to @pin, notice: 'Pin created' }
else
format.html { redirect_to root_url }
end
end
end
def destroy
@article = Article.find(params[:article_id])
@pin = @article.pins.find(params[:id])
@pin.destroy
respond_to do |format|
format.html { redirect_to root_url }
end
end
지금 여기 내 두 가지 질문 :
- 현재 관계를 삭제하는 양식은 어떻게 만듭니 까?
- 내 양식에 버튼 중 하나만 보여주고 싶습니다. 조건부로 올바른 버튼을 표시하려면 어떻게합니까?