1

I have been trying to post data to controller action in RAILS application development. When i post data through form, POST data reaches specific controller action defined in routes.rb. But it gives back with 404 error.

Here is code:

Controller->

class UserActivityController < ApplicationController
unloadable

def index
   #Defining form
   @userActivity = UserActivity.new
end

def create
    puts "Helloo"
    @user_activity = UserActivity.new
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @user_activity }
    end
end

routes.rb ->

match 'timesheet/create' => 'userActivity#create', :via => :post, :as=>:create

and I tried with

post 'timesheet/create', :to => 'userActivity#create'

Form ->

<%= form_for @userActivity,:url => { :controller=>"userActivity",:action => "create" }, :remote => true do |task| %>
[.....]
<% end %>

Error :

Started POST "/timesheet/create" for 127.0.0.1 at 2015-07-19 11:12:58 +0530
Processing by UserActivityController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"qsqRIGH8caCz4I25m1sFKviELHEYOWNUsiLYNnc1kP8=", "date"=>"2015-07-19", "project"=>"", "task"=>{"detail"=>""}, "time"=>{"start(5i)"=>"", "end(5i)"=>""}, "issue"=>"", "commit"=>"Create"}
Helloo
Current user: XXX (id=XX)
Missing template, responding with 404
  Rendered common/error.html.erb (0.6ms)
Completed 404 Not Found in 7ms (Views: 1.8ms | ActiveRecord: 0.0ms)

Please help me to figure out this issue!.

Prasanth P
  • 715
  • 2
  • 9
  • 24
  • 2
    Your form has a `remote:true` and your create action has a `format.json` which shouldn't be. I guess Rails is looking for `create.js.erb` and that's why you are getting missing template error. – Pavan Jul 19 '15 at 06:32
  • Thanks for suggestion, It is my mistake, I missed to create html.erb file for that controller action function. But i did that purpose fully. Because, In PHP and all, when post data using jquery and return will be in json, right?. so, i thought there is no need of template in that function. – Prasanth P Jul 19 '15 at 07:20

1 Answers1

1

Where is the parameters to create the UserActivity? You can see from the log that user with ID(XX). Also, after submitting to a post method you should use redirect_to or create a create.js file to respond it with js.

Emu
  • 5,763
  • 3
  • 31
  • 51
  • I missed HTML template in controller function. user details which you notified is some other query related redmine tasks. I have posted parameter as POST data, so no need to define in routes.rb file. – Prasanth P Jul 19 '15 at 07:24