samedi 26 octobre 2019

Is there a simple way I can query a Ruby on Rails join table in my view

New to RoR and struggling to make my table join work in my application views. I have 2 models Category and Pricing. They are joined through CategoryPricings .

Concept is a single Category can have multiple Pricings ( 4 pricing levels for the category ).

Where I am struggling is presenting the pricing information in my view when a category is selected.

So if I chose category (with id = 3) I would like the 4 price levels for that category to be presented.

My Models

 create_table "categories", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "pricings", force: :cascade do |t|
    t.string "overview"
    t.text "description"
    t.integer "delivery_time"
    t.integer "price"
    t.integer "pricing_type"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "category_pricings", force: :cascade do |t|
    t.bigint "category_id", null: false
    t.bigint "pricing_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["category_id"], name: "index_category_pricings_on_category_id"
    t.index ["pricing_id"], name: "index_category_pricings_on_pricing_id"
  end

My Controllers

class UsersController < ApplicationController

  before_action :authenticate_user!

  def dashboard
    #@user = User.find(params[:id])
    @categories = Category.find(params[:id])
  end

  def show
    @user = User.find(params[:id])
    @user_listings = @user.listings.paginate(:page => params[:page], :per_page => 5)
  end

  def index
    @user = User.paginate(:page => params[:page], :per_page => 5 )
  end

  def set_user
    @user = User.find(params[:id])
  end


  def update
    @user = current_user
    if @user.update_attributes(current_user_params)
      flash[:notice] = "Saved"
    else
      flash[:alert] = "Cannot update..."
    end
    redirect_to users_dashboard_path
  end

  private
  def current_user_params
    params.require(:user).permit(:from, :about, :status, :language, :avatar)
  end

  def category_params
    params.require(:category).permit(pricings: [])
  end

  protected

  # If you have extra params to permit, append them to the sanitizer.
  def configure_sign_up_params
    devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:full_name, :username, :email, :password, :password_confirmation)}
  end

  # If you have extra params to permit, append them to the sanitizer.
  def configure_account_update_params
    devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
  end

  #The path used after sign up.
  def after_sign_up_path_for(resource)
    users_dashboard_path
  end

  def after_sign_in_path_for(resource)
    users_dashboard_path
  end

  def update_resource(resource, params)
    resource.update_without_password(params)
  end
end


  # def after_sign_in_path_for(resource)
  #   users_dashboard_path
  # end

  # The path used after sign up for inactive accounts.
  # def after_inactive_sign_up_path_for(resource)
  #   super(resource)
  # end

My Views Code

<footer class="card-footer">
    <% @categories.name %>

      <a class="has-text-danger is-block card-footer-item has-text-right">

      </a>
  </footer>

I am unfortunately not able get my view working so I can get the the list of prices for each category. Any help appreciated

Aucun commentaire:

Enregistrer un commentaire