Ask A Question

Notifications

You’re not receiving notifications from this thread.

Problems with Routes and Forms (Am I a Newb? What am I missing here?)

shakycode asked in General

I'm working on my TimeClock functionality for a legacy Rails 3.2.21 app and am running into problems with simple crud type forms.

I'm trying to create a new ClockEvent entry using a basic form in Rails. When I submit the form I raise an exception in the dev log:

undefined methodclock_events_path' for #<#Class:0x007feadcbbf250:0x007feadcbef298>`

Here is what my code looks like:

new.html.erb

<h4>New TimeCard Entry </h4>
<%= form_for (@clock_event) do |ce| %>
    <%= ce.collection_select(:user_id, User.employee.order("username ASC"), :id, :username, {:include_blank => true}, {:class => 'select', required: true}) %>
    <%= ce.label "Clock In" %>
    <%= ce.date_select :clock_in, order: [:month, :day, :year] %></br>
    <%= ce.time_select :clock_in, :combined => true, :minute_interval => 1, :start_hour => 00, :end_hour => 23 %>
    <%= ce.label "Clock Out" %>
    <%= ce.date_select :clock_out, order: [:month, :day, :year] %></br>
    <%= ce.time_select :clock_out, :combined => true, :minute_interval => 1, :start_hour => 00, :end_hour => 23 %>
    <%= ce.label "Station" %>
    <%= ce.collection_select(:station_id, Station.order("station_name ASC"), :id, :station_name, {:include_blank => true}, {:class => 'select', required: true}) %>

    <%= ce.button  'Create Entry', class: 'btn btn-info', data: {disable_with: "<i class='icon-spinner'></i>Creating..."} %>
<% end %>

timecards_controller

class TimecardsController < ApplicationController

  def index
    @users = User.employee.order("username ASC")
  end

  def new
    @clock_event = ClockEvent.new
  end

  def create
    parse_times!
    @clock_event = ClockEvent.new(params[:timecard])

     if @clock_event.save
       redirect_to clock_event_path, notice: "Entry added for #{@clock_event.user.username}".html_safe
      else
       render :new, notice: "Time Card Entry failed to Save".html_safe
      end
  end

  def show
    @user = User.find(params[:id])
    @clock_events = @user.clock_events.order("created_at ASC").paginate(:per_page => 10, :page => params[:page])
  end

  def edit
    @user = User.find(params[:id])
    @clock_events = @user.clock_events.order("created_at ASC").paginate(:per_page => 10, :page => params[:page])
  end

  def update
    @clock_event = ClockEvent.find(params[:clock_event][:id])
    if @clock_event.update_attributes(params[:clock_event])
        redirect_to edit_timecard_path(@clock_event.user.id), notice: "Updated Successfully".html_safe
    else
        redirect_to :back, notice: "Woops.".html_safe
    end
  end

  private

  def parse_times!
    params[:timecard].parse_time_select! :clock_in if params[:timecard].has_key? 'clock_in(5i)'
    params[:timecard].parse_time_select! :clock_out if params[:timecard].has_key? 'clock_out(5i)'
  end
end

routes.rb

resources :timecards

output of rake routes

 timecards GET    /timecards(.:format)                 timecards#index
                           POST   /timecards(.:format)                 timecards#create
              new_timecard GET    /timecards/new(.:format)             timecards#new
             edit_timecard GET    /timecards/:id/edit(.:format)        timecards#edit
                  timecard GET    /timecards/:id(.:format)             timecards#show
                           PUT    /timecards/:id(.:format)             timecards#update
                           DELETE /timecards/:id(.:format)             timecards#destroy

In my new.html.erb I'm passing the @clock_event object that's defined under the controller's new method. But it keeps giving me the

undefined methodclock_events_path' for #<#Class:0x007feadcbbf250:0x007feadcbef298>` error.

Should I be manually passing a URL path and a method for this since the instance variable does not match the controller convention? And if so, what URL should I be passing?

This seems like I'm making some newb mistakes here but I've had an AirPair expert help me with something similar and we had to do some really ugly passing of URL paths with ID to get it to work.

I'm open to feedback, criticism, and slaps on the back of the head.

Reply

Ok, I think I figured this out on my own.

The params that were being passed from the form was not [:timecard] but instead was [:clock_event]

So in my create action in the timecards_controller.rb I changed the following to get the right params hash.

def create
    parse_times!
    @clock_event = ClockEvent.new(params[:clock_event])

     if @clock_event.save
       redirect_to timecard_path(@clock_event.user.id), notice: "Entry added for #{@clock_event.user.username}".html_safe
      else
       render :new, notice: "Time Card Entry failed to Save".html_safe
      end
  end

private
  def parse_times!
    params[:clock_event].parse_time_select! :clock_in if params[:clock_event].has_key? 'clock_in(5i)'
    params[:clock_event].parse_time_select! :clock_out if params[:clock_event].has_key? 'clock_out(5i)'
  end

In the form I needed to pass the explicit url and the method of post to get this to work since I'm working with an object outside of the Rails namespace convention

<%= form_for @clock_event, url: "/timecards", method: :post do |ce| %>

Voila... It's working!

Disregard guys...

Reply

The best kinds of questions are the ones you ask and answer yourself shortly afterwards. <3

Reply

When the brainfog clears and the caffeine kicks in, it's go time :)

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.