Angela Inniss
Angela Inniss

Reputation: 359

My submit button is not working on my form_for in Rails - no error message

Hello I am back for my second question.

My submit button on my form_for does not do anything when I click on it. I did have an error message in the console that said 'Unpermitted parameter: :photo_cache' however when I saw this I permitted the 'photo_cache' in the params in my controller, BUT the submit button on my form still doesn't work.

Context: I am trying to create a hairdressers which has the following params: name, description, location, photo and address.

Any help would be appreciated, thanks!

My form:

<%= simple_form_for(@hairdresser) do |f| %>
  <%= f.error_notification %>
  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
  <!-- [...] -->

  <div class="form-inputs">
    <%= f.input :name %>
  </div>

  <div class="form-inputs">
    <%= f.input :address %>
  </div>

  <div class="form-inputs">
    <%= f.input :location %>
  </div>

   <div class="form-inputs">
    <%= f.input :description %>
  </div>

  <div class="form-inputs">
  <%= f.input :photo %>
  <%= f.input :photo_cache, as: :hidden %>
   </div>

  <div class="form-actions">
    <%= f.button :submit, label: "Submit Form", class: "btn btn-primary"  %>
  </div>
  <!-- [...] -->
<% end %>

My controller:

class HairdressersController < ApplicationController
  def index
    @hairdressers = Hairdresser.all
  end

  def show
    @hairdresser = Hairdresser.find(params[:id])
  end

  def new
    @hairdresser = Hairdresser.new
  end

  def create
    @hairdresser = Hairdresser.new(hairdresser_params)
    # @hairdresser.save ? (redirect_to hairdresser_path(@hairdresser)) : (render 'new')
      if @hairdresser.save
        redirect_to hairdresser_path(@hairdresser)
      else
        render 'new'
      end
  end

  def edit
    @hairdresser = Hairdresser.find(params[:id])
  end

  def update
    @hairdresser =  Hairdresser.find(params[:id])
  end

  def destroy
    @hairdresser =  Hairdresser.find(params[:id])
  end
end
private

def hairdresser_params
  params.require(:hairdresser).permit(:name, :address, :photo, :location, :description, :photo_cache)
end

My model:

class Hairdresser < ApplicationRecord
  belongs_to :user
  validates :name, presence: true
  validates :location, presence: true
  validates :description, presence: true
  validates :location, presence: true

  mount_uploader :photo, PhotoUploader
end

Upvotes: 0

Views: 613

Answers (1)

Angela Inniss
Angela Inniss

Reputation: 359

Hi I actually found an answer to this online. Here is the answer for anyone who has lost time with this issue! :

"If you're on Rails 5, you'll need to update your user association to:

belongs_to :user, optional: true"

Upvotes: 1

Related Questions