Ruby on rails 更新或重定向的Rails路由
我不确定我是否做对了。我有一个操作,如果用户登录,我想复制、创建和保存一个新对象;如果用户未登录,我想重定向。我在这里不使用表单,因为我使用的是样式化按钮,其图像如下所示:Ruby on rails 更新或重定向的Rails路由,ruby-on-rails,routes,Ruby On Rails,Routes,我不确定我是否做对了。我有一个操作,如果用户登录,我想复制、创建和保存一个新对象;如果用户未登录,我想重定向。我在这里不使用表单,因为我使用的是样式化按钮,其图像如下所示: <a href="/lists/add/<%= @list.id %>" class="button"> <span class="add_list">Learn these words</span> </a> def add if is_
<a href="/lists/add/<%= @list.id %>" class="button">
<span class="add_list">Learn these words</span>
</a>
def add
if is_logged_in?
list = logged_in_user.copy_list(params[:id])
if list.save
flash[:notice] = "This list is now in your stash."
redirect_to stash_zoom_nav_quiz_path(list, "zoomout", "new", "quizoff")
else
flash[:notice] = "There was a problem adding this list."
redirect_to :back
end
else
redirect_to :controller => "users", :action => "signup_and_login", :list_id => params[:id]
end
end
map.resources :lists, :collection => {:share => :get, :share_callback => :get, :add => :put}
我把这个动作作为一个:放在我的路线上,我不确定这是否正确,或者其他东西是否是正确的方法。感谢您的帮助。在您的路线中尝试此功能。rb:
map.resources :lists, :member => {:add => :put}, :collection => {:share => :get, :share_callback => :get}
:member-与:collection相同,但用于操作特定成员的操作。您的问题的具体答案是
map.resources :lists, :collection => { :share => :get, :share_callback => :get }, :member => { :add => :put }
add
操作对成员有效,而不是对集合有效
但代码中还有其他问题。首先,您应该始终使用Rails帮助程序来生成URL。实际上,路径/lists/add/
是错误的。它应该是/lists//add
改变
<a href="/lists/add/<%= @list.id %>" class="button">
<span class="add_list">Learn these words</span>
</a>
出于好奇,将成员错误标记为集合会有什么负面影响?同样感谢您的帮助,我不知道您可以创建带有链接的块!有一件事,我想我应该在这里使用POST方法b/c我正在创建一个新记录,但是,如果我使用POST或PUT,我会得到一个“ActionController::UnknownAction”路由错误。看来GET是唯一一种说单词的方法,这样可以吗?
<% link_to add_list_path(@list), :class => "button" do %>
<span class="add_list">Learn these words</span>
<% end %>
class MyController < ActionController::Base
before_filter :require_logged_user, :only => %w( add )
def add
list = logged_in_user.copy_list(params[:id])
if list.save
flash[:notice] = "This list is now in your stash."
redirect_to stash_zoom_nav_quiz_path(list, "zoomout", "new", "quizoff")
else
flash[:notice] = "There was a problem adding this list."
redirect_to :back
end
end
protected
def require_logged_user
if !is_logged_in?
redirect_to :controller => "users", :action => "signup_and_login", :list_id => params[:id]
end
end
end