2011-10-27 105 views
0

導軌忽略我的重定向。導軌忽略我的重定向

使用案例:

  1. 用戶導航到他的帳戶,並選擇選項,以顯示他的訂閱狀態
  2. 應用列出了他目前的訂閱和選項取消它
  3. 用戶取消訂閱
  4. 應用程序更新訂閱記錄並將用戶重定向回訂閱動作

    - >但鐵路忽略這最後一步......任何想法我做錯了什麼?

路線

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> 
+0

發現的bug在我嵌套的觀點'訂閱'表格中的'取消訂閱'表格 - 沒有任何意義,我重新安排了我的觀點(將兩種形式分開,一種放在'如果'部分,另一種放在'其他'部分。 – hebe

+0

將盡快stackoverflows允許它張貼新視圖.. – hebe

回答

0

使用redirect_to代替render(在你的unsubscribe行動最後的方法調用) 。

+0

感謝您的輸入。對不起,我實際上嘗試redirect_to(並沒有工作)。我也試過以前渲染(這也不工作)但忘了以取代它。所以:redirect_to不起作用 – hebe

0

找到了我在'subscribe'表單中嵌套'unsubscribe'表單的錯誤 - 沒有任何意義。

我重新安排我的觀點(分離這兩種形式,一是在「如果」部分,在「其他」部分的其他現在工作正常。

<div class = 'wrapper'> 
    <%= render :partial => "my_account_leftbar" %> 
    <% @all_subscriptions.each do |subscription| %> 
    <div class="field">Start: <%= subscription.started_at %></div><br><br> 
    <% end %> 

    <% if @subscription.new_record? %> 
    <% form_for @subscription, :url => subscribe_user_path(current_user) do |f| %> 
     <div class="field"> 
      <%= f.check_box :subscription_type %> 
      <div class="actions"> 
      <%= f.submit "Subscribe", :class => "button mr8" %> 
      </div> 
     </div> 
    <% end %> 
    <% else -%> 
    <% form_for @subscription, :url => unsubscribe_user_path(current_user) do |f| %> 
    <%= f.check_box :subscription_type, :value => @subscription.subscription_type, :disabled => true %> 
    <div class="actions"> 
     <%= f.submit "Unsubscribe", :class => "button mr8" %> 
    </div> 
    <% end %> 
    <% end %> 
</div>