开发者

Ruby on Rails: routing problem: action is not seen in link_to

Just trying to set up some basic links with authlogic. here they are

<% if current_user %>
    <div class="register_new_user">
        <%= link_to "Register", new_user_path %>
    </div>
    <div class="login_existing_user">
        <%= link_to "Log In", new_user_session_path %>
    </div>

<% else %>
    <div class="logout">
        <%= link_to "Log Out", :controller => :user_sessions, :action => :destroy %>
    </div>
<% end %>

what is also weird is that destroy_开发者_运维知识库user_session_path doesn't exist, and is what I would prefer to use. But when I click the Logout Link, it just takes me to localhost:3000/user_sessions/

here is my user_sessions controller:

class UserSessionsController < ApplicationController
  before_filter :require_no_user, :only => [:new, :create]
  before_filter :require_user, :only => :destroy

  def new
    @user_session = UserSession.new
  end

  def create
    @user_session = UserSession.new(params[:user_session])
    if @user_session.save
      flash[:notice] = "Login successful!"
      redirect_back_or_default account_url
    else
      render :action => :new
    end
  end

  def destroy
    current_user_session.destroy
    flash[:notice] = "Logout successful!"
    redirect_back_or_default new_user_session_url
  end
end

so, I'm confused as to why new_user_session_path works, and destroy_user_session_path doesn't.

for reference: my routes:

  map.resource :user_session
  map.resource :account, :controller => "users"
  map.resources :users

  map.root :controller => "info", :action => "home"
  map.connect "/home", :controller => "info", :action => "home"


Do this in your view:

<%= link_to "Log Out", user_session_path, :method => :delete %>

The reason you can't just have a destroy_user_session_path method is because that would be a GET request, which breaks RESTful routing convention. GET requests are only meant to retrieve data, not create, update, or destroy it.

This should fix your issue.


For UPDATE and DELETE you need to provide the method as a parameter:

<%= link_to "Log Out", user_session_path, :method => :delete %>


I think you should use instead:

<%= link_to "Log Out", :controller => :user_session, :action => :destroy %>

Notice the controller in singular...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜