samedi 4 mai 2019

How to update Model attribute in Rails

SO I have this app, that let's users order shirt online.

This is how the item order looks, it has shirt size, shirt color, phone number, address, and order status.

enter image description here

I was modifying the existing code so that the admin users can change the order status from "ordered" into "processing", or "delivered" by selecting an option from a dropdown list beside the order status or to make it easier using a textfield by manually writing the supposed status, and click on submit button.

I get the view part, but I'm having difficulty with the update method in the controller, I don't know where to start being a novice rails coder myself.

SO this is my Model

class Micropost < ApplicationRecord
   belongs_to :user
   default_scope -> { order(created_at: :desc) }
   mount_uploader :picture, PictureUploader
   validates :user_id, presence: true
   validates :shirtSize, presence: true
   validates :shirtColor, presence: true
   validates :contactAddress, presence: true
   validates :contactNumber, presence: true
   validate  :picture_size
   #uncomment picture presence validation in the future
   # validates :picture, presence: true

   SIZE_LIST = [ " ", "S", "M", "L", "XL" ]
   COLOR_LIST = [ " ", "Black", "White", "Gray", "Red", "Green", "Blue", "Navy Blue", "Yellow", "Pink"]

   private

   # Validates the size of an uploaded picture.
   def picture_size
       if picture.size > 5.megabytes
          errors.add(:picture, "should be less than 5MB")
       end
   end
 end

VIEW

<li id="micropost-<%= micropost.id %>">
<%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
<span class="user"><%= link_to micropost.user.name, micropost.user %> 
</span>
<span class="content">
<%= "#{micropost.shirtSize}, #{micropost.shirtColor}" %>
<% if current_user?(micropost.user) || current_user.admin? %><br/>
  <%= "#{micropost.contactNumber} | #{micropost.contactAddress}" %> 
  <br/>
  <h4>Status: <%= micropost.orderStatus %></h4>
<% end %>
</span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
<% if current_user?(micropost.user) && micropost.orderStatus == "Ordered" %>
<%= link_to "Cancel", micropost, method: :delete,
                                 data: { confirm: "You sure?" } %>
<% end %>
</span>
<span class="content">
<%= image_tag micropost.picture.url if micropost.picture? %>
</span>
</li>

CONTROLLER

class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy, :update]
before_action :correct_user,   only: :destroy
before_action :admin_user,     only: :update

def create
  @micropost = current_user.microposts.build(micropost_params)
  if @micropost.save
    flash[:success] = "Design Posted!"
    redirect_to root_url
  else
    @feed_items = []
    render 'static_pages/home'
  end
end

def destroy
  @micropost.destroy
  flash[:success] = "Design deleted"
  redirect_to request.referrer || root_url
end

def update
  #Have no idea what to do here...
end

private

  def micropost_params
    defaults = { orderStatus: 'Ordered' }
    params.require(:micropost).permit(:shirtSize, :shirtColor, :contactNumber, :contactAddress, :picture).merge(defaults)
  end

  def correct_user
    @micropost = current_user.microposts.find_by(id: params[:id])
    redirect_to root_url if @micropost.nil?
  end

  # Confirms an admin user.
  def admin_user
    redirect_to(root_url) unless current_user.admin?
  end

end

Aucun commentaire:

Enregistrer un commentaire