0

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.

  • can you show the response header from your browser when you submit the form? Also do the fields in your form correlate to the params in your controller? – Jax Oct 02 '14 at 11:14
  • Yes, the fields in the form are the same of 'store_params' function. Request parameters {"utf8"=>"✓", "authenticity_token"=>"vdBK16ZwnqAPWtqTOlNjA5lBIHnW9Vb5WkpbOYhoCrQ=", "commit"=>"Add store", "action"=>"create", "controller"=>"stores"} – Alex Angelini Oct 02 '14 at 11:23
  • Below what you posted, in your browser you should see if what you submit has any values, it's under the Form Data and should look like this: name: xxxxxxx, description: xxxxxx, also make sure all the fields in this bit correspond to your permitted params – Jax Oct 02 '14 at 11:28
  • This is the form: <%= form_for @store do |f| %>
    <%= f.label 'Nome negozio' %> <%= f.text_field :name, class: 'form-control', maxlength: 20 %>
    <%= f.label 'Descrizione' %> <%= f.text_area :description, class: 'form-control' %>
    <%= f.label 'Indirizzo' %> <%= f.text_field :address, class: 'form-control' %>
    etc etc <% end %> and there are no form data in better_error page
    – Alex Angelini Oct 02 '14 at 11:42
  • look at the browser response and make sure the form is submitting data as per above comment. If this is correct try changing your params as per this: params.fetch(:store, {}).permit(:name, :description, :address, :telephone, :email, :website) – Jax Oct 02 '14 at 11:56
  • it seems like the form doesn't submit data; how can i fix it? @Jax700303 – Alex Angelini Oct 02 '14 at 12:41
  • where is your form, is it a partial from a "new" view? – Jax Oct 02 '14 at 12:58
  • yes, it's a _form partial used in new view – Alex Angelini Oct 02 '14 at 13:02
  • submitting the form doesn't pass parameters. Any ideas? @Jax700303 – Alex Angelini Oct 02 '14 at 19:17
  • Do you have a post method for say resources :store in your routes? Also can you show the entire browser response pls? Grazie Mile – Jax Oct 02 '14 at 20:09
  • In the routes file i have "resource :stores" only. The entire broswer response is here http://pastebin.com/TszaC6et @Jax700303 – Alex Angelini Oct 02 '14 at 21:08
  • from the logs your form is not posting, try adding to your routes something like "post 'store/create' => 'store#create' " – Jax Oct 02 '14 at 21:41
  • I'm looking for an error in the form, but i can't find it. Route 'post .....' doesn't work. @Jax700303 – Alex Angelini Oct 02 '14 at 21:54
  • 1
    Seems like a fixed this mergin partial _form.html.erb in new.html.erb – Alex Angelini Oct 02 '14 at 22:01

1 Answers1

0

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'
Jason
  • 13
  • 3