Editing Products
The process of editing records is very similar to creating records. Instead of
new and create actions, we will have edit and update.
Let’s implement them in the controller with the following:
class ProductsController < ApplicationController def index @products = Product.all end
def show @product = Product.find(params[:id]) end
def new @product = Product.new end
def create @product = Product.new(product_params) if @product.save redirect_to @product else render :new, status: :unprocessable_entity end end
def edit @product = Product.find(params[:id]) end
def update @product = Product.find(params[:id]) if @product.update(product_params) redirect_to @product else render :edit, status: :unprocessable_entity end end
private def product_params params.expect(product: [ :name ]) endendNext we can add an Edit link to :
<h1><%= @product.name %></h1>
<%= link_to "Back", products_path %><%= link_to "Edit", edit_product_path(@product) %>
Proudly built by Evil Martians based on the Rails Guides.
Files
Preparing Environment
- Preparing Ruby runtime
- Prepare development database