Rails - Создание связи между двумя моделями не позволяет мне редактировать модель - PullRequest
0 голосов
/ 06 июня 2018

Я пытаюсь создать связь между двумя моделями в моем приложении на Rails (Пользователь и Монета), где Coin принадлежит пользователю и пользователю has_many Coins.Когда я добавляю ассоциацию own_to в модель Coin, я больше не могу редактировать или создавать страницы Coin.Зачем это делать?Как только я удаляю связь, я снова могу создавать / редактировать.Кроме того, соответствующая ассоциация has_many на странице пользователя не имеет такого же эффекта.Я был бы признателен за любую помощь в понимании того, что здесь происходит и как я могу правильно сделать эту ассоциацию.Спасибо.

User.rb

class User < ApplicationRecord
  acts_as_votable
  has_many :questions, dependent: :destroy
  has_many :events, dependent: :destroy
  has_many :links, dependent: :destroy
  has_many :posts, dependent: :destroy
  has_many :moderated_coins, class_name: "Coin"

  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable,
     :validatable, authentication_keys: [:login]

  validates :username, presence: :true, uniqueness: { case_sensitive: false }
  validates_format_of :username, with: /^[a-zA-Z0-9_\.]*$/, :multiline => true
  validate :validate_username

  def validate_username
    if User.where(email: username).exists?
      errors.add(:username, :invalid)
    end
  end

  def login=(login)
    @login = login
  end

  def login
    @login || self.username || self.email
  end

  def self.find_for_database_authentication(warden_conditions)
    conditions = warden_conditions.dup
    if login = conditions.delete(:login)
      where(conditions.to_h).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
    elsif conditions.has_key?(:username) || conditions.has_key?(:email)
      where(conditions.to_h).first
    end
  end
end

Coin.rb

class Coin < ApplicationRecord
  validates :currency_name, presence: true
  has_many :questions, dependent: :destroy
  has_many :events, dependent: :destroy
  has_many :links, dependent: :destroy
  mount_uploader :picture, PictureUploader
  has_and_belongs_to_many :genres
  # belongs_to :moderator, class_name: "User".  <--- * The problem is here
  validate :picture_size

  private

    def picture_size
      if picture.size > 5.megabytes
        errors.add(:picture, "Picture must be smalled than 5MB.")
      end
    end
end

coins_controller.rb

class CoinsController < ApplicationController

  load_and_authorize_resource param_method: :question_params
  before_action :find_coin, only: [:edit, :update, :destroy ]
  before_action :authenticate_user!, except: [:index, :create, :show]

  def index
    @search = Coin.ransack(params[:q])
    @coins = @search.result(distinct: true)
  end

  def new
    @coin = Coin.new
  end

  def create
    @coin = Coin.new(coin_params)
    if @coin.save
      flash[:success] = "Coin created"
      redirect_to @coin
    else
      render 'new'
    end
  end

  def show
    @coin = Coin.find(params[:id])
  end

  def edit
    authorize! :update, @coin
  end

  def update
    if @coin.update(coin_params)
      redirect_to @coin
    else
      render 'edit'
    end            
  end

def destroy
  Coin.find(params[:id]).destroy
  redirect_to coins_url
end

private

  def coin_params
    params.require(:coin).permit( :currency_name, :currency_abbrev, :moderator_id, :accepted, :picture, :question1, :question2, :question3, :question4, genre_ids:[])
  end

  def find_coin
    @coin = Coin.find(params[:id])
  end

end

user_controller.rb

class UsersController < ApplicationController

  before_action :authenticate_user!

  def show
    @user = User.find(params[:id])
    respond_to do |format|
        format.html # show.html.erb
        format.xml { render :xml => @user }
    end
  end
end

schema.rb

create_table "coins", force: :cascade do |t|
  t.string "link_name"
  t.string "currency_name"
  t.string "currency_abbrev"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string "permalink"
  t.boolean "accepted", default: false
  t.datetime "accepted_at"
  t.string "genre"
  t.integer "genre_id"
  t.integer "moderator_id"    
end

create_table "users", force: :cascade do |t|
  t.string "email", default: "", null: false
  t.string "encrypted_password", default: "", null: false
  t.string "reset_password_token"
  t.datetime "reset_password_sent_at"
  t.datetime "remember_created_at"
  t.integer "sign_in_count", default: 0, null: false
  t.datetime "current_sign_in_at"
  t.datetime "last_sign_in_at"
  t.string "current_sign_in_ip"
  t.string "last_sign_in_ip"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string "username"
  t.string "wallet"
  t.boolean "admin", default: false
  t.boolean "moderator", default: false
  t.decimal "currentbalance", precision: 8, scale: 2
  t.decimal "payout_to_date", precision: 8, scale: 2
  t.text "bio"
  t.string "link1"
  t.string "link2"
  t.string "link3"
  t.string "link4"
  t.string "link5"
  t.string "name"
  t.index ["email"], name: "index_users_on_email", unique: true
  t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  t.index ["username"], name: "index_users_on_username", unique: true
end

1 Ответ

0 голосов
/ 06 июня 2018

Использование:

belongs_to :moderator, class_name: "User", optional: true

В рельсах 5 belongs_to по умолчанию обеспечивает наличие связанной записи.Вам нужно использовать optional: true, чтобы moderator_id был равен nil.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...