導軌忽略我的重定向。導軌忽略我的重定向
使用案例:
- 用戶導航到他的帳戶,並選擇選項,以顯示他的訂閱狀態
- 應用列出了他目前的訂閱和選項取消它
- 用戶取消訂閱
應用程序更新訂閱記錄並應將用戶重定向回訂閱動作
- >但鐵路忽略這最後一步......任何想法我做錯了什麼?
路線
map.resources :users, ..., :member => { ..., :subscriptions => :get, :subscribe => :post, :unsubscribe => :put}
控制器:
def subscriptions
@tradesman = User.find_by_id(params[:id])
@subscription = @tradesman.current_subscription || Subscription.new
@all_subscriptions = Subscription.find(:all)
end
def subscribe
@tradesman = User.find_by_id(params[:id])
@subscription = current_user.subscriptions.build(params[:subscription])
@subscription.update_attributes(:started_at => Time.zone.now)
@subscription.save
redirect_to :action => 'subscriptions', :id => @tradesman.id
end
def unsubscribe
@tradesman = User.find_by_id(params[:id])
@subscription = @tradesman.current_subscription
@subscription.update_attributes(:ended_at => Time.zone.now)
@subscription.save
redirect_to :action => 'subscriptions', :id => @tradesman.id
end
查看:
<div class = 'wrapper'>
<%= render :partial => "my_account_leftbar" %>
<% form_for @subscription, :url => subscribe_user_path(current_user) do |f| %>
<% @all_subscriptions.each do |subscription| %>
<div class="field">Start: <%= subscription.started_at %></div><br><br>
<% end %>
<% if @subscription.new_record? %>
<div class="field">
<%= f.check_box :subscription_type %>
<div class="actions">
<%= f.submit "Subscribe", :class => "button mr8" %>
</div>
</div>
<% else -%>
<%= f.check_box :subscription_type, :value => @subscription.subscription_type, :disabled => true %>
<% form_for @subscription, :url => unsubscribe_user_path(current_user) do |f| %>
<div class="actions">
<%= f.submit "Unsubscribe", :class => "button mr8" %>
</div>
<% end %>
<% end %>
<% end %>
</div>
發現的bug在我嵌套的觀點'訂閱'表格中的'取消訂閱'表格 - 沒有任何意義,我重新安排了我的觀點(將兩種形式分開,一種放在'如果'部分,另一種放在'其他'部分。 – hebe
將盡快stackoverflows允許它張貼新視圖.. – hebe