Jakxna360 Jakxna360 - 1 year ago 65
Ruby Question

Error when editing a page: param is missing or the value is empty: static_page

I am having a hard time creating the routes to my pages. I have show which works but edit keeps popping up with an error. The current code is a modification of this railscast along with the friendly ID gem. Thank you if you can help.

Here is the controller:

class StaticPagesController < ApplicationController
before_action :set_static_page, only: [:show, :edit, :update, :destroy]
before_filter :except => [:show] do
redirect_to :new_user_session unless current_user && current_user.role == 5

# GET /static_pages
# GET /static_pages.json
def index
@static_pages = StaticPage.all

# GET /static_pages/1
# GET /static_pages/1.json
def show

# GET /static_pages/new
def new
@static_page = StaticPage.new

# GET /static_pages/1/edit
def edit
@static_page = StaticPage.find_by_slug(params[:slug])

# POST /static_pages
# POST /static_pages.json
def create
@static_page = StaticPage.new(static_page_params)

respond_to do |format|
if @static_page.save
format.html { redirect_to @static_page, notice: 'Static page was successfully created.' }
format.json { render :show, status: :created, location: @static_page }
format.html { render :new }
format.json { render json: @static_page.errors, status: :unprocessable_entity }

# PATCH/PUT /static_pages/1
# PATCH/PUT /static_pages/1.json
def update
respond_to do |format|
if @static_page.update(static_page_params)
format.html { redirect_to @static_page, notice: 'Static page was successfully updated.' }
format.json { render :show, status: :ok, location: @static_page }
format.html { render :edit }
format.json { render json: @static_page.errors, status: :unprocessable_entity }

# DELETE /static_pages/1
# DELETE /static_pages/1.json
def destroy
respond_to do |format|
format.html { redirect_to static_pages_url, notice: 'Static page was successfully destroyed.' }
format.json { head :no_content }

# Use callbacks to share common setup or constraints between actions.
def set_static_page
@static_page = StaticPage.find_by_slug(params[:slug])

# Never trust parameters from the scary internet, only allow the white list through.
def static_page_params
params.require(:static_page).permit(:title, :permalink, :content, :tags, :author, :date)

Here is the route:

Rails.application.routes.draw do

resources :static_pages, except: [:show, :delete, :update]

devise_for :users

mount Bootsy::Engine => '/bootsy', as: 'bootsy'

get 'profile' => "users#profile"

root 'indexes#index'

get ':slug', to: 'static_pages#show'

get ':slug', to: 'static_pages#delete'

get ':slug/edit', to: 'static_pages#update'

# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

# You can have the root of your site routed with "root"
# root 'welcome#index'

# Example of regular route:
# get 'products/:id' => 'catalog#view'

# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products

# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
# collection do
# get 'sold'
# end
# end

# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end

# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end

# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable

# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end

Here is the form where the error keeps poping up on form.html.haml:

= simple_form_for(@static_page) do |f|
= f.error_notification

= f.input :title
= f.input :permalink
= f.bootsy_area :content, :class => 'form-control', rows: 12
= f.input :tags
= f.input :author
= f.input :date

= f.button :submit

Answer Source

In your routes you have:

get ':slug/edit', to: 'static_pages#update'

Where you're sending a GET request to your update action, rather than your edit action.

Change that to:

get ':slug/edit', to: 'static_pages#edit'

You're also setting the same get :slug route to two different actions as well. The one pointing to the destroy action should be a DELETE request rather than a GET request.

One more thing, you can remove @static_page = StaticPage.find_by_slug(params[:slug]) from your controller edit action, since you're already setting the @static_page variable in your set_static_page in your before_action.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download