Rails忽略了我的重定向。
用例:
app更新订阅记录, 将用户重定向回订阅操作
- >但铁路忽略了最后一步......任何想法我做错了什么?
路线
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 :(得分:0)
使用redirect_to
代替render
(unsubscribe
操作中的最后一次方法调用)。
答案 1 :(得分:0)
在视图中找到了我在'subscribe'表单中嵌套'unsubscribe'表单的错误 - 什么都没有意义。
我重新安排了我的观点(将这两个表格分开,一个在'if'部分,另一个在'else'部分。现在工作正常:
<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>