Witam.
Jakiś czas temu zacząłem naukę Rail’ów, przerabiając tutorial Hartla. Niestety, napotkałem problem(rozdział 10: sam początek podpunktu 10.2 Tutorial) (jak mniemam z mojej, a nie autora winy ). Szukałem odpowiedzi na stack’u znalazłem ale w tym momencie już tak namieszałem, że sam nie wiem co jest co… (Chyba udało mi się przywrócić stan do momentu, z którego wyszedłem ale mogłem jeszcze coś dodatkowo skopać). Czy mógłby ktoś mi pomóc to ogarnąć??
Kod testu powodującego błąd:
require ‘test_helper’
class UsersEditTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test “unsuccessful edit” do
get edit_user_path(@user)
assert_template ‘users/edit’
patch user_path(@user), params: { user: { name: “”,
email: “foo@invalid”,
password: “foo”,
password_confirmation: “bar” } }
assert_template 'users/edit'
end
test “successful edit” do
log_in_as(@user)
get edit_user_path(@user)
log_in_as(@user)
assert_template ‘users/edit’
name = “Foo Bar”
email = "foo@bar.com"
patch user_path(@user), user: { name: name,
email: email,
password: ‘’,
password_confirmation: ‘’ }
assert_not flash.empty?
assert_redirected_to @user
@user.reload
assert_equal @user.name, name
assert_equal @user.email, email
end
end
Kontroler User’ow
class UsersController < ApplicationController
before_action :logged_in_user, only: [:edit, :update]
def new
@user=User.new
end
def show
@user=User.find(params[:id])
end
def create
@user = User.new(user_params)
if @user.save
log_in @user
flash[:success]="Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
def edit
@user=User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
# Handle a successful update.
else
render 'edit'
end
end
# Before filters
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in."
redirect_to login_url
end
end
end
**Widok edit **
<% provide(:title, ‘Edit user’) %>
<% provide(:button_text, ‘Save changes’) %>
daje hash’a bo zamienia…
<#h1>Update your profile
<#div class=“row”>
<#div class=“col-md-6 col-md-offset-3”>
<#%= render ‘form’ %>
<#div class=“gravatar_edit”>
<#%= gravatar_for @user %>
<#a href=“http://gravatar.com/emails” target="_blank">Change
</#div>
</#div>
</#div>
Partial:
<%= form_for(@user) do |f| %>
<%= render ‘shared/error_messages’, object: @user %>
<%= f.label :name %>
<%= f.text_field :name, class: ‘form-control’ %>
<%= f.label :email %>
<%= f.email_field :email, class: ‘form-control’ %>
<%= f.label :password %>
<%= f.password_field :password, class: ‘form-control’ %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: ‘form-control’ %>
<%= f.submit yield(:button_text), class: “btn btn-primary” %>
<% end %>
routes.rb
Rails.application.routes.draw do
root ‘static_pages#home’
get ‘/help’, to: ‘static_pages#help’
get ‘/about’, to: ‘static_pages#about’
get ‘/contact’, to: ‘static_pages#contact’
get ‘/signup’, to: ‘users#new’
get ‘/login’, to: ‘sessions#new’
post ‘/login’, to: ‘sessions#create’
delete ‘/logout’, to: ‘sessions#destroy’
resources :users
For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Z góry dzięki za pomoc.