1

My Rails form ("new" action in my Controller) is not creating the object when I click the Submit button. I don't get any errors in the page or in console. It seems like it's stuck and not performing a POST to the "create" action in my controller. When I click the Submit button on my form, the URL changes to meds/new?utf8=✓&authenticity_token=<random characters> and clears the form.

I feel like I'm missing something really basic (it's been a while since I programmed in Rails).

Here's the relevant code:

new.html.erb

<% if user_signed_in? && current_user.is_admin == true %>
                    <%= simple_form_for(@med) do |f| %>
                          <div class="form-group">
                                <%= f.label :brand_name, "Name of the medicine:" %>
                                <%= f.text_field :brand_name, required: true %>
                          </div>
                          <div class="form-group">
                          <%= f.label :generic_name %>
                          <%= f.text_field :generic_name, required: true %>
                          </div>
                          <div class="form-group">
                                <%= f.label :description %>
                                <%= f.input :description, as: :summernote, :id => 'description' %>
                          </div>
                          <div class="form-group">
                                <%= f.label :reactions %>
                                <%= f.text_area :reactions, class: 'summernote', id: 'reactions' %>
                          </div>
                          <div class="form-group">
                             <%= f.label :interactions %>
                                <%= f.text_area :interactions, class: 'summernote', id: 'interactions' %>
                          </div>
                          <div class="form-group">
                                <%= f.label :implementation %>
                                <%= f.text_area :implementation, class: 'summernote', id: 'implementation' %>
                          </div>
                          <div class="form-group">
                                <%= f.label :availability %>
                                <%= f.text_area :availability, class: 'summernote', id: 'availability' %>
                          </div>
                          <div class="form-group">
                                <%= f.label :warnings %>
                                <%= f.text_area :warnings, class: 'summernote', id: 'warnings' %>
                          </div>
                    <%= f.submit "Save changes", class: "btn btn-primary" %>
                     <% end %>
              <% end %>

meds_controller.rb

class MedsController < ApplicationController
  def index
  end

  def new
    @med = Med.new
  end

  def create
    @med = Med.create(med_params)
    redirect_to root_url
  end

  def show
    @med = Med.find(params[:id])
  end

  def edit
    @med = Med.find(params[:id])
  end

  def update
    @med = Med.find(params[:id])
        respond_to do |format|
            if @med.update(med_params)
                format.html { redirect_to @med, notice: 'Med was successfully updated.' }
                format.json { head :no_content }
            else
                format.html { render action: 'edit' }
                format.json { render json: @med.errors, status: :unprocessable_entity }
            end
        end
    end

  private

  def med_params
    params.require(:med).permit(:brand_name, :generic_name, :description, :reactions, :interactions, :implementation,
        :availability, :warnings)
  end

end

meds.rb model

class Med < ActiveRecord::Base
end

routes.rb

Rails.application.routes.draw do
  devise_for :users
  resources :meds
  #get 'meds/index'

  root to: "meds#index"
end

What did I miss?

Thanks!!

EDIT: Added output in console when I click the button:

Started GET "/meds/new?utf8=%E2%9C%93&authenticity_token=XG44W3%2Bj5A3piAJnp0uQUF8L5gruoGDgdiaqZgCSf3bY219l%2BGIRc4Z%2FaypXa%2FacacQGTCFQmFMckS30Jf6ZLg%3D%3D&med%5Bbrand_name%5D=sfsdf&med%5Bgeneric_name%5D=sdfsdf&med%5Bdescription%5D=sdfsdf&med%5Breactions%5D=&med%5Binteractions%5D=&med%5Bimplementation%5D=&med%5Bavailability%5D=&med%5Bwarnings%5D=&commit=Save+changes" for ::1 at 2015-08-28 22:24:00 -0400
Processing by MedsController#new as HTML

EDIT 2: I don't think create is being called. Here's my rake routes:

                  Prefix Verb   URI Pattern                    Controller#Action
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                    meds GET    /meds(.:format)                meds#index
                         POST   /meds(.:format)                meds#create
                 new_med GET    /meds/new(.:format)            meds#new
                edit_med GET    /meds/:id/edit(.:format)       meds#edit
                     med GET    /meds/:id(.:format)            meds#show
                         PATCH  /meds/:id(.:format)            meds#update
                         PUT    /meds/:id(.:format)            meds#update
                         DELETE /meds/:id(.:format)            meds#destroy
                    root GET    /                              meds#index
winston
  • 3,000
  • 11
  • 44
  • 75
  • Can you show more of `new.html.erb` ? I don't see the submit button. – SteveTurczyn Aug 29 '15 at 00:34
  • Sorry about that! Fixed it above – winston Aug 29 '15 at 00:54
  • Try to save the model with `Med.create!(med_params)` in the `MedsController#create` action, and check if you have any validation error. You can as well put a debugger and check the state of this object. – mrodrigues Aug 29 '15 at 01:04
  • Thanks for the suggestion. Just gave it a shot but I didn't get any errors – winston Aug 29 '15 at 01:05
  • Are you sure that the create action is being called? Did you put a debugger inside it and it stopped there? – mrodrigues Aug 29 '15 at 02:57
  • I think you're right. Looking at my console it looks like it never even calls the create action. I posted my rake routes above...looks ok I think. Not sure why create isn't being called – winston Aug 29 '15 at 03:12

2 Answers2

0

Try this in your create action:

def create
  @med = Med.create(med_params)

  if @med.save
    flash[:notice] = "Med was successfully created"
    redirect_to med_path(@med)
  else
    flash[:error] = "Med was not successfully created"
    render :new
  end
end

and add this to the first line of your index.html.erb

<p id="notice">
  <%= notice %>
</p>
  • Didn't work. The notice did work though. It showed when I signed in (using devise) but it didn't show an error after I clicked the Submit button. Nothing happens except the URL changes. – winston Aug 29 '15 at 02:07
  • yeah, definitely not hitting your create action. I'm betting its related to this http://stackoverflow.com/questions/4104474/rails-3-utf-8-query-string-showing-up-in-url – Benjamin Linville Aug 29 '15 at 03:35
  • I'd try @PaulAnnesley's answer from that page – Benjamin Linville Aug 29 '15 at 03:38
  • he says: form_tag in Rails 4.2 (and probably earlier) has a :enforce_utf8 option; If set to false, a hidden input with name utf8 is not output. and he has links – Benjamin Linville Aug 29 '15 at 03:40
  • Thanks again for the help. I added `<%= f.text_field :brand_name, required: true, :enforce_utf8 => false %>` to my text fields and I still get the issue. I'm so stumped. – winston Aug 29 '15 at 12:23
  • Could it have something to do with Javascript? I didn't see any errors in my Javascript console, but I'm wondering if it doesn't like my summernote field – winston Aug 29 '15 at 14:32
  • Try this: Comment out the entire new.html.erb, then type "hello_world" at the bottom in plain text. (hello_world should be the only thing NOT commented-out) Refresh your page. hello_world should render without error. Next, leave the form_for stuff commented out but uncomment this line: `<% if user_signed_in? && current_user.is_admin == true %>` (now only "hello_world" & `<% if user_signed_in? && current_user.is_admin == true %>` are uncommented) refresh the page. If utf error returns, its because of the authenticity check on that line. if not, its because of something in the form_for stuff. – Benjamin Linville Sep 03 '15 at 17:09
0

try this in your new.html.erb

<%= simple_form_for @med, :url => { :action => "create" } do |f| %>
anoop0302
  • 21
  • 3