Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 设计在新的自定义操作后不登录_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 设计在新的自定义操作后不登录

Ruby on rails 设计在新的自定义操作后不登录,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,嗨,我是rails的新手,在尝试添加新的post方法时遇到了麻烦 我想创建一个新方法来启用用户 因此,我修改了我的用户控制器,使其具有以下方法 def enable if (!@user.enabled) @user.enabled = true; if @user.save UserMailer.send_enabled_message(@user).deliver redirect_to users_url, notice: 'User was successful

嗨,我是rails的新手,在尝试添加新的post方法时遇到了麻烦

我想创建一个新方法来启用用户

因此,我修改了我的用户控制器,使其具有以下方法

def enable

if (!@user.enabled)
  @user.enabled = true;
  if @user.save
    UserMailer.send_enabled_message(@user).deliver
    redirect_to users_url, notice: 'User was successfully enabled.'
  else
    redirect_to users_url, notice: 'Could not enable user.'
  end
else
redirect_to users_url, notice: 'Could not enable user.'
end
结束

我在我的路线中添加了以下内容:

match 'users/:id' => 'users#enable', :via => 'post', :action => :enable
并将以下内容添加到显示用户表的视图中:

<td><%= link_to 'Enable', user, method: :enable, data: { confirm: 'Are you sure?' } %></td>
我的看法是:

<td><%= link_to 'Enable', enable_user_path(user), method: :enable, data: { confirm: 'Are you sure?' } %></td>

只需在routes.rb中更改路线定义,即可回答您的所有问题。删除您添加的行,并在下面您可能有:

resources :users
将其更改为:

resources :users do
  post 'enable', :on => :collection
end
这将在资源级别为集合(so,/users)添加处理程序,而不是成员(/users/2)。它由用户控制器中的“enable”方法自动处理

所以,第一个答案——是的,您正在匹配发布给用户的所有URL,所以这确实覆盖了登录发布。如上所述的定义将按照您想要的方式进行路由

第二个答案,你是如何得到URL的?好的,现在它是这样路由的,您可以运行rake路由来查看定义的路径是什么。现在,我不麻烦你了:

enable_users_path
因此,您可以:

link_to 'enable', enable_users_path
同样,通过以上述方式定义路由,将为您生成此路径方法。您还可以通过一个属性来定义这个方法,如果您遇到这个问题的话

因此,如果您进行上述更改,您应该:

  • 能够重新登录(以及向用户发布任何其他方法)
  • 有一个方法使\u用户\u路径能够获取URL
  • 能够发布到/用户/启用
  • 那会让你走的。有关路由的详细信息:
    只需在routes.rb中更改路线定义,即可回答您的所有问题。删除您添加的行,并在下面您可能有:

    resources :users
    
    将其更改为:

    resources :users do
      post 'enable', :on => :collection
    end
    
    这将在资源级别为集合(so,/users)添加处理程序,而不是成员(/users/2)。它由用户控制器中的“enable”方法自动处理

    所以,第一个答案——是的,您正在匹配发布给用户的所有URL,所以这确实覆盖了登录发布。如上所述的定义将按照您想要的方式进行路由

    第二个答案,你是如何得到URL的?好的,现在它是这样路由的,您可以运行rake路由来查看定义的路径是什么。现在,我不麻烦你了:

    enable_users_path
    
    因此,您可以:

    link_to 'enable', enable_users_path
    
    同样,通过以上述方式定义路由,将为您生成此路径方法。您还可以通过一个属性来定义这个方法,如果您遇到这个问题的话

    因此,如果您进行上述更改,您应该:

  • 能够重新登录(以及向用户发布任何其他方法)
  • 有一个方法使\u用户\u路径能够获取URL
  • 能够发布到/用户/启用
  • 那会让你走的。有关路由的详细信息: