【问题标题】:Couldn't find Suscription without an ID找不到没有 ID 的订阅
【发布时间】:2021-09-02 09:40:34
【问题描述】:

我有一个应用程序,用户使用设计进行身份验证,在用户模型中,我在数据库中添加了一个名为 admin 的列,默认值为 false。这样我就设法以管理员身份访问应用程序的某些部分。 我有一个订阅模型,每个用户在经过身份验证后都会默认获得一个免费值。我想要实现的是用户列表中的管理员用户可以从免费切换到高级。这是我拥有的代码,我无法让它工作。

用户模型:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  
  #Validaciones
  validates :nombre, :apellido, presence: true
  devise :database_authenticatable, :validatable, password_length: 8..128
  

  #Relaciones
  has_many :patients, dependent: :destroy
  has_many :articles, dependent: :destroy
  has_one :profile, dependent: :destroy
  has_one :suscription, dependent: :destroy
  
 
 
  #Creación de perfil
  after_create :set_profile
  def set_profile
       self.profile = Profile.create()  
  end      
  
  #Creación de suscripcion
  after_create :set_suscription
  def set_suscription
       self.suscription = Suscription.create()  
  end  

end

订阅模式:

class Suscription < ApplicationRecord
  belongs_to :user

  enum status: {
    free:     0,
    premium:  1
  }
end

用户控制器:

class UsersController < ApplicationController
    def index
        @pagy, @users = pagy(User.order(created_at: :asc), items:12)
    end
    
    def show
        @user = User.find(params[:id])
    end
    
    
    
end

Suscriptios 控制器:

class SuscriptionsController < ApplicationController
  before_action :set_suscription
  
  def show
    
  end

  def edit
  end

  def update
    @suscription = Suscription.find(params[:id]).update_params
    redirect_to profile_path
    flash[:notice] = "La suscripción ha sido actualizada"
  end

  private
  def set_suscription
    @suscription = (current_user.suscription ||= Suscription.create)
  end

  def suscription_params
    params.require(:suscription).permit(:status)
  end
end

路线: #更新高级版

patch "suscriptions", to:"suscriptions#update", as: "user_premium"

查看(链接):

<%= link_to 'Update', user_premium_path ,method: :patch %>

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    这应该可以解决它:

    subscriptions_controller.rb

    def update
      @suscription = Suscription.find(params[:id]).update(subscription_params)
      redirect_to profile_path
      flash[:notice] = "La suscripción ha sido actualizada"
    end
    

    查看

    <%= link_to 'Update', user_premium_path(id: @subscription.id, status: "premium"), method: :patch %>
    

    另一个不需要的东西,但通常我会在控制器中看到类似的东西:

    private
    
      def set_suscription
        @suscription = Suscription.find(params[:id])
      end
    

    然后使您的更新方法如下所示:

    def update
      @subscription.update(subscription_params)
      redirect_to profile_path
      flash[:notice] = "La suscripción ha sido actualizada"
    end
    

    这一切都假设您只是尝试使用您的 link_to 将订阅从免费更新为高级。我不建议这样做,因为如果有人不小心标记了这个怎么办?他们不能再回到免费订阅。也许有一个模式打开,通过下拉选择状态被路由到订阅编辑会更好?

    【讨论】:

    • 是的,我认为这不是最好的方法,但我想测试一下admin用户是否可以更新状态。无论如何,视图给了我这个错误: undefined method `id 'for nil: NilClass
    • 不用担心。是的,我假设您可以访问视图中的订阅。您只需要在您的 link_to 中传递订阅 ID。如果您发布您的整个视图,我可以帮助您使用 link_to。只是不确定你在循环什么。如果是用户,那么您可以轻松访问订阅 ID。
    • 我的视图只包含 link_to: 我根据您向我提出的建议进行了更正
    • 是的,我可以访问订阅 ID,我一直这样,但我的错误在控制器中。 这里我得到这个错误:param is missing or value is empty: subscription def suscription_params params.require(:订阅).permit(:状态)结束
    猜你喜欢
    • 1970-01-01
    • 2018-06-05
    • 2016-07-08
    • 2019-12-28
    • 2011-09-07
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 2014-06-13
    相关资源
    最近更新 更多