angularjs - Angular js , How to upload image using paperclip in Rails API? -
hi trying create application using rails , angular js . here have api marketplace , using gem called paperclip . in general work perfect means in simple rails trying add image using angularjs . here want upload image using paperclip . attaching codes below have done far . here marketplace model :
class marketplace < activerecord::base belongs_to :user has_attached_file :image, styles: {large: "600x600",medim: "300x300",thumb:"150x150#" } validates_attachment_content_type :image, :content_type => /\aimage\/.*\z/ validates :name, :tittle, :description, presence: true end
and marketplace controller:
class marketplacescontroller < applicationcontroller before_filter :authenticate_user!, :except => [:show, :index] #before_action :set_market_place, only: [:show] before_action :marketplace_owner, only: [:edit, :update, :destroy] skip_before_action :verify_authenticity_token # /market_places # /market_places.json # add method def marketplace_owner @market_place = marketplace.find(params[:id]) unless @market_place.user_id == current_user.id flash[:notice] = 'access denied not owner of job' redirect_to market_places_path end end def index @market_places = marketplace.all respond_to |format| format.html # your-action.html.erb end end # /market_places/1 # /market_places/1.json def show @message = message.new @market_places = marketplace.find(params[:id]) respond_to |format| format.html format.json{@market_places} end end # /market_places/new def new @market_place = marketplace.new end # /market_places/1/edit def edit @market_place = marketplace.find(params[:id]) end # post /market_places # post /market_places.json def create @market_place = marketplace.new(market_place_params) respond_to |format| if @market_place.save format.html { redirect_to @market_place, notice: 'market place created.' } format.json { render :show, status: :created, location: @market_place } else format.html { render :new } format.json { render json: @market_place.errors, status: :unprocessable_entity } end end end # patch/put /market_places/1 # patch/put /market_places/1.json def update @market_place = marketplace.find(params[:id]) respond_to |format| if @market_place.update(market_place_params) format.html { redirect_to @market_place, notice: 'market place updated.' } format.json { render :show, status: :ok, location: @market_place } else format.html { render :edit } format.json { render json: @market_place.errors, status: :unprocessable_entity } end end end # delete /market_places/1 # delete /market_places/1.json def destroy @market_place.destroy respond_to |format| format.html { redirect_to market_places_url, notice: 'market place destroyed.' } format.json { head :no_content } end end private # use callbacks share common setup or constraints between actions. def set_market_place @market_place = marketplace.find(params[:id]) end # never trust parameters scary internet, allow white list through. def market_place_params params.require(:market_place).permit(:user_id, :name, :tittle, :description, :image) #params[:market_place] end end
here in angular can send tittle, description , name using angular code :
mainapp.controller("marketplacecontroller", ['$scope', "$http", function($scope, $http) { $http.get("/api/v1/market_places").success(function(response) {$scope.names = response;}); $scope.addentry = function(){ $scope.names.push({ 'name':$scope.name,'title':$scope.tittle, 'description': $scope.description, }); // writing server // var dataobj = { name : $scope.name, tittle : $scope.tittle, description : $scope.description, image : $scope.image, }; // posting data php file $http({ method : 'post', url : '/market_places.json', data : dataobj, //forms user object headers : {'x-csrf-token': $('meta[name=csrf-token]').attr('content')} }) .success(function(data) { if (data.errors) { // showing errors. $scope.errorname = data.errors.name; $scope.errorusername = data.errors.username; $scope.erroremail = data.errors.email; } else { $scope.message = data.message; } }); $scope.tittle=''; $scope.description=''; }; }]);
this have done far on net unable find clear solution explain how peperclip work angular me tahnks in advance .
Comments
Post a Comment