[Rails]以用户

时间:2016-11-20 09:47:00

标签: ruby-on-rails ruby authentication

我训练使用铁轨。我已经为用户创建了一个身份验证系统,当然,他可以访问此信息。但如果他有联系,他可以更新他的信息。现在,我有一个问题,修补程序的重定向工作但是没有更新他的信息例程伪。用户放了一个新伪,但它没有更新。但它适用于控制台。

控制器会话代码:

class SessionsController < ApplicationController
  def new
  end

def show
    @page = User.find(params[:id])

end

def update
    User.find(params[:id]).update name: params[:name]
    redirect_to "/profil/#{current_user.id}"
end

  def create
    user = User.find_by_email(params[:email])
    # If the user exists AND the password entered is correct.
    if user && user.authenticate(params[:password])
      # Save the user id inside the browser cookie. This is how we keep the user
      # logged in when they navigate around our website.
      session[:user_id] = user.id
      redirect_to '/'
    else
    # If user's login doesn't work, send them back to the login form.
      redirect_to '/login'
    end
  end

  def destroy
    session[:user_id] = nil
    redirect_to '/login'
  end
end

show的代码:

<h1>Profil</h1>
 
<% if current_user && @page.id == current_user.id %>
<%= form_tag "/profil/#{@page.id}", method: "patch" do %>
<ul>
    <li><input type="text" name="name" value="<%= current_user.name %>" /></li>
    <li><%= image_tag current_user.avatar.url(:thumb) %></li>
  <input type="submit" value="Enregistrer mon profil" />
</ul>
<% end
else
 %>
<%= @page.name %>
<br>
<%= image_tag @page.avatar.url(:thumb) %>
 
<% end %>

路线代码:

Rails.application.routes.draw do
  get 'sessions/new'

  get 'users/new'

  # For details on the DSL available within this file, see <a href="http://guides.rubyonrails.org/routing.html" rel="nofollow">http://guides.rubyonrails.org/routing.html</a>

    get '/signup' => 'users#new'
    post '/users' => 'users#create'

      get '/login' => 'sessions#new'
  post '/login' => 'sessions#create'
  get '/logout' => 'sessions#destroy'

  get '/profil/:id' => 'sessions#show'
  patch '/profil/:id' => 'sessions#update'
end

经过大量的测试,我没有发现我的错误。

对不起,如果我的英语不好,因为我是法国人。

提前谢谢^^

0 个答案:

没有答案