简体   繁体   中英

Rails: param is missing or the value is empty. strong parameters in controller

i have this controller

class StoresController < ApplicationController
  before_filter :authenticate_business!, :except => [:index, :show]

def index
  #@stores = Store.paginate(:page => params[:page])#, :per_page => 8)

  if params[:query].present?
    @stores = Store.search(params[:query], page: params[:page])
  else
    @stores = Store.all.page params[:page]
  end
end

def show
  @store = Store.friendly.find(params[:id])

  if request.path != store_path(@store)
    redirect_to @store, status: :moved_permanently
  end
end

def new
  @store = Store.new
end

def create
  @store = Store.new(store_params)

  @store.business_id = current_business.id

  if @store.save
    redirect_to @store
  else
    render 'new'
  end
end

def edit
  @store = Store.friendly.find(params[:id])
end

def update
  @store = Store.friendly.find(params[:id])

  if @store.update(store_params)
    redirect_to @store
  else
    render 'edit'
  end
end

def destroy
  @store = Store.friendly.find(params[:id])

  @store.destroy

  redirect_to stores_url
end


private
  def store_params
    params.require(:store).permit(:name, :description, :address, :telephone, :email, :website)
  end

end

and a view with a form to create a new store.

<%= form_for @store do |f| %>

.......
code
......

<% end %>

The problem is that when i submit the form, it gives me this error "param is missing or the value is empty: store", pointing at line "params.require(:store).permit(:name, :description, :address, :telephone, :email, :website)"

Any idea to solve this problem? Thank you.

I had this same issue and it was caused by a route issue, as discussed in the comments, causing the form not to post any data.

I think what you need is to make sure 'get' requests to the 'new' route access your 'new' method, while 'post' requests to the 'new' route access your 'create' method. Something like:

get   'stores/new'  => 'stores#new'
post  'stores/new'  => 'stores#create'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM