Answer the question
In order to leave comments, you need to log in
`
Undefined method `user_id' for #. What is the problem?
I need to make sure that only the author of the post can edit it.
I just recently started learning Rails and the backend in general.
I tried to fix it for a long time, but it did not work.
models
class Task < ApplicationRecord
validates :title, :content, presence: true
has_rich_text :content
end
class User < ApplicationRecord
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
validates :login, presence: true
validates :fullname, presence: true
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }, allow_blank: true
# Возвращает дайджест для указанной строки.
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ?
BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
# Возвращает случайный токен.
def User.new_token
SecureRandom.urlsafe_base64
end
# Запоминает пользователя в базе данных для использования в постоянных сеансах.
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end
# Возвращает true, если указанный токен соответствует дайджесту.
def authenticated?(remember_token)
return false if remember_digest.nil?
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end
# Забывает пользователя
def forget
update_attribute(:remember_digest, nil)
end
validates :address, presence: true
validates :city, presence: true
validates :state, presence: true
validates :country, presence: true
validates :zip, presence: true
validates :role, presence: true
# Активирует учетную запись.
def activate
update_attribute(:activated, true)
update_attribute(:activated_at, Time.zone.now)
end
# Посылает письмо со ссылкой на страницу активации.
def send_activation_email
UserMailer.account_activation(self).deliver_now
end
# Устанавливает атрибуты для сброса пароля.
def create_reset_digest
self.reset_token = User.new_token
update_attribute(:reset_digest, User.digest(reset_token))
update_attribute(:reset_sent_at, Time.zone.now)
end
# Посылает письмо со ссылкой на форму ввода нового пароля.
def send_password_reset_email
UserMailer.password_reset(self).deliver_now
end
# Возвращает true, если время для сброса пароля истекло.
def password_reset_expired?
reset_sent_at < 2.hours.ago
end
private
# Преобразует адрес электронной почты в нижний регистр.
def downcase_email
self.email = email.downcase
end
# Создает и присваивает токен активации и его дайджест.
def create_activation_digest
self.activation_token = User.new_token
self.activation_digest = User.digest(activation_token)
end
end
class TasksController < ApplicationController
before_action :set_task, only: [:show, :edit, :update, :destroy]
#before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
def index
@tasks = Task.all
end
def show
end
def new
@task = Task.new
end
def edit
end
def create
@task = Task.new(task_params)
@task.user_id = current_user.id
respond_to do |format|
if @task.save
format.html { redirect_to @task, notice: 'Задание опубликовано!' }
format.json { render :show, status: :created, location: @task }
else
format.html { render :new }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
end
def update
if @task.user_id == current_user.id
respond_to do |format|
if @task.update(task_params)
format.html { redirect_to @task, notice: 'Задание обновлено!' }
format.json { render :show, status: :ok, location: @task }
else
format.html { render :edit }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
else
redirect_to root_path, notice: 'Нет доступа!'
end
end
def destroy
@task.destroy
respond_to do |format|
format.html { redirect_to tasks_url, notice: 'Задание удалено!' }
format.json { head :no_content }
end
end
private
def set_task
@task = Task.find(params[:id])
end
def task_params
params.require(:task).permit(:title, :content)
end
end
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
def index
@users = User.paginate(page: params[:page])
end
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
log_in @user
# @user.send_activation_email (log_in @user нужно удалить)
# flash[:info] = "Пожалуйста, проверьте вашу электронную почту для активации аккаунта."
redirect_to root_url
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Профиль обновлён!"
redirect_to @user
else
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "Пользователь удалён"
redirect_to users_url
end
private
def user_params
params.require(:user).permit(:login, :fullname, :email,
:address, :city, :state, :country, :zip, :role,
:password, :password_confirmation)
end
# Предварительные фильтры
# Подтверждает права пользователя.
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
end
ActiveRecord::Schema.define(version: 2020_12_02_210215) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "action_text_rich_texts", force: :cascade do |t|
t.string "name", null: false
t.text "body"
t.string "record_type", null: false
t.bigint "record_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["record_type", "record_id", "name"], name: "index_action_text_rich_texts_uniqueness", unique: true
end
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.bigint "record_id", null: false
t.bigint "blob_id", null: false
t.datetime "created_at", null: false
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
end
create_table "active_storage_blobs", force: :cascade do |t|
t.string "key", null: false
t.string "filename", null: false
t.string "content_type"
t.text "metadata"
t.bigint "byte_size", null: false
t.string "checksum", null: false
t.datetime "created_at", null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end
create_table "tasks", force: :cascade do |t|
t.string "title"
t.text "content"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "users", force: :cascade do |t|
t.string "login"
t.string "fullname"
t.string "email"
t.string "address"
t.string "city"
t.string "state"
t.string "country"
t.string "zip"
t.string "role"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "password_digest"
t.string "remember_digest"
t.boolean "admin", default: false
t.string "activation_digest"
t.boolean "activated", default: false
t.datetime "activated_at"
t.string "reset_digest"
t.datetime "reset_sent_at"
t.index ["email"], name: "index_users_on_email", unique: true
end
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
end
class CreateTasks < ActiveRecord::Migration[6.0]
def change
create_table :tasks do |t|
t.string :title
t.text :content
t.timestamps
end
end
end
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :login
t.string :fullname
t.string :email
t.string :address
t.string :city
t.string :state
t.string :country
t.string :zip
t.string :role
t.timestamps
end
end
end
Answer the question
In order to leave comments, you need to log in
connection between User and Task models is not established: rusrails.ru/active-record-associations#svyaz-belongs_to
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question