Я делаю фитнес-приложение в рамках проекта. В этом проекте 5 моделей. Пользователь, Muscle_Groups, Диета, Тренировки и Питание. Ассоциации указаны в коде ниже. На данный момент у меня есть новая страница и страница показа для пользователя. Я хочу перенаправить пользователя со страницы показа на индексную страницу muscle_group, где он перечислит все мышцы в теле человека. У пользователя, очевидно, есть много Muscle_Groups, я хочу заполнить страницу индекса muscle_group всеми группами мышц (бицепс, спина, ноги, грудь). Проблема в том, что мне нужно создать эти экземпляры с помощью user_id текущего пользователя с помощью приложения, и я не знаю, как это сделать на данный момент. Я надеюсь, что мое краткое объяснение поможет, мой код ниже.
class User < ApplicationRecord
has_many :diets
has_many :muscle_groups
before_save { self.email = email.downcase }
#before saving, the email is lowercased
validates :name, presence: true, length: { maximum: 50 }
#validates the names presence, max char length is 50
validates :weight, presence: true, numericality: true
validates :height, presence: true, numericality: true
validates_inclusion_of :gender, :in => %w( m f male Male female Female)
validates :age, presence: true, numericality: {only_integer: true }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
end
class Muscle_Group < ApplicationRecord
has_many :workouts
belongs_to :user
end
class Diet < ApplicationRecord
belongs_to :user
has_many :meals
end
class Meal < ApplicationRecord
belongs_to :diet
end
class Workout < ApplicationRecord
belongs_to :muscle_group
end
class UsersController < ApplicationController
def new #render's the sign up page for a new user
@user = User.new
end
def create #action to create the user
@user = User.create(user_params)
if @user.save
log_in @user
flash[:success] = "Are you ready to GitFit?!"
redirect_to @user #redirects to the user's show page, which is the main menu
else
render 'new'
end
end
def show
@user = User.find(params[:id])
end
private
def user_params
params.require(:user).permit(:name,
:email,
:weight,
:height,
:gender,
:age,
:password,
:password_confirmation)
end
end
class Muscle_GroupsController < ApplicationController
def index
@muscle_groups = Muscle_Group.all
end
end