Wednesday, 15 August 2012

ruby - How to Pass object into filters method in rails -



ruby - How to Pass object into filters method in rails -

i want pass object filter , want alter object attributes using after_filter on create action . have product model having attributes name,title,price.once product has been added want set alter cost using after_filter how can pass created @product object filter method , alter cost .

class productscontroller < applicationcontroller before_action :set_product, only: [:show, :edit, :update, :destroy] after_action :change_price, only: [:create] //what should here..? # /products # /products.json def index @products = product.all end # /products/1 # /products/1.json def show end # /products/new def new @product = product.new end # /products/1/edit def edit end # post /products # post /products.json def create @product = product.new(product_params) respond_to |format| if @product.save format.html { redirect_to @product, notice: 'product created.' } format.json { render action: 'show', status: :created, location: @product } else format.html { render action: 'new' } format.json { render json: @product.errors, status: :unprocessable_entity } end end end .. //rest of code .. private # utilize callbacks share mutual setup or constraints between actions. def set_product @product = product.find(params[:id]) end # never trust parameters scary internet, allow white list through. def product_params params.require(:product).permit(:name, :category_id) end def change_name //what should here..? no status want alter every cost 10$ end end

you should utilize model callback after_create accomplish this

ruby-on-rails ruby ruby-on-rails-4

No comments:

Post a Comment