Ruby on Rails: Associations User to Orders -


only admin can create orders. orders assigned user user_id. on users profile page, place user can see records. have feeling problem controller. i'm new, please bare me. thanks!

    class orderscontroller < applicationcontroller   before_action :set_order, only: [:show, :edit, :update, :destroy]    # /orders   # /orders.json   def index     @orders = order.all   end    # /orders/1   # /orders/1.json   def show    end    # /orders/new   def new     @order = user.find_by(params[:id]).orders.build   end    # /orders/1/edit   def edit   end    # post /orders   # post /orders.json   def create     @order = current_user.orders.build(order_params)       respond_to |format|       if @order.save         format.html { redirect_to @order, notice: 'order created.' }         format.json { render :show, status: :created, location: @order }       else         format.html { render :new }         format.json { render json: @order.errors, status: :unprocessable_entity }       end     end   end    # patch/put /orders/1   # patch/put /orders/1.json   def update     respond_to |format|       if @order.update(order_params)         format.html { redirect_to @order, notice: 'order updated.' }         format.json { render :show, status: :ok, location: @order }       else         format.html { render :edit }         format.json { render json: @order.errors, status: :unprocessable_entity }       end     end   end    # delete /orders/1   # delete /orders/1.json   def destroy     @order.destroy     respond_to |format|       format.html { redirect_to orders_url, notice: 'order destroyed.' }       format.json { head :no_content }     end   end    private     # use callbacks share common setup or constraints between actions.     def set_order       @order = order.find(params[:id])     end      # never trust parameters scary internet, allow white list through.     def order_params       params.require(:order).permit(:service, :charge, :user_id)     end end 

this users's controller

   class userscontroller < applicationcontroller      def index         @users = user.all     end      def new         @user = user.new     end      def create           customer = stripe::customer.create(     :email => 'example@stripe.com',     :card  => params[:stripetoken]   )            rescue stripe::carderror => e       flash[:error] = e.message       redirect_to charges_path       end      def admin_dashboard         @users = user.all     end       def show      end      def edit     end      def update         if params[:user][:password].blank?       params[:user].delete(:password)       params[:user].delete(:password_confirmation)         end          token = params[:stripetoken]         customer = stripe::customer.create(             card: token,             email: current_user.email              )          current_user.stripeid = customer.id         current_user.save         redirect_to dashboard_path     end      def payment     end      def destroy     end      private      def user_params         params.require(:user).permit( :stripe_card_token, :avatar, :first_name, :last_name, :country_code, :phone_number,:home_adress,:work_address, :email, :password, :current_password)     end   end 

model user

class user < activerecord::base   # include default devise modules. others available are:   # :confirmable, :lockable, :timeoutable , :omniauthable   devise :database_authenticatable, :registerable,          :recoverable, :rememberable, :trackable, :validatable    has_many :orders   end 

order model

class order < activerecord::base     belongs_to :user end 

profile page

<div class = 'container'>     <table class="table table-hover" style = 'width: 70%'>             <thead>                 <tr>                     <th> id </th>                     <th> date </th>                     <th>service</th>                     <th>charge</th>                     <th>feedback</th>                 </tr>             </thead>     <tbody>         <% order.all.each |orders| %>      <tr>           <td><%= orders.user.id %></td>           <td><%= orders.created_at %></td>           <td><%= orders.service %></td>           <td><%= orders.charge %></td>     </tr>       <%end%> 

if i'm missing please let me know. i'm not sure how associate admin class of this.


Comments

Popular posts from this blog

What Are The Best Universities In The World?

Hard vs. Soft Water: What's The Difference?

java - Solr query version issue: Invalid version or the data in not in 'javabin' format -