Ruby NoMethodError - undefined method `blah_url 'for BlahController

I call this js from the link:

function createNewTopLevelEntry(){ var user_id = $("#user").val(); var header = prompt("Enter the name"); $.ajax( '/users/' + user_id + '/entries', { data: { entry: { header: header, user: user_id } }, type: 'POST', cache: false, dataType: 'json', success: displayTopLevelEntries }); } 

He refers to this controller:

 def create @entry = Entry.new(params[:entry]) respond_to do |format| if @entry.save format.html { redirect_to @entry, notice: 'Entry was successfully created.' } format.json { render json: @entry, status: :created, location: @entry } else format.html { render action: "new" } format.json { render json: @entry.errors, status: :unprocessable_entity } end end end 

This is the response on the server:

 Started POST "/users/1/entries" for 127.0.0.1 at 2013-03-25 21:50:36 -0700 Processing by EntriesController#create as JSON Parameters: {"entry"=>{"header"=>"Hi", "user"=>"1"}, "user_id"=>"1"} (0.1ms) begin transaction SQL (0.5ms) INSERT INTO "entries" ("completed", "created_at", "endtime", "header", "parent", "starttime", "starttimeset", "text", "totaltime", "updated_at", "user") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["completed", nil], ["created_at", Tue, 26 Mar 2013 04:50:36 UTC +00:00], ["endtime", nil], ["header", "Hi"], ["parent", nil], ["starttime", nil], ["starttimeset", nil], ["text", nil], ["totaltime", nil], ["updated_at", Tue, 26 Mar 2013 04:50:36 UTC +00:00], ["user", "1"]] (2.5ms) commit transaction Completed 500 Internal Server Error in 10ms NoMethodError - undefined method `entry_url' for #<EntriesController:0x007fb22b9f7fd8>: (gem) actionpack-3.2.11/lib/action_dispatch/routing/polymorphic_routes.rb:129:in `polymorphic_url' (gem) actionpack-3.2.11/lib/action_dispatch/routing/url_for.rb:150:in `url_for' (gem) actionpack-3.2.11/lib/action_controller/metal/rendering.rb:60:in `_process_options' (gem) actionpack-3.2.11/lib/action_controller/metal/streaming.rb:208:in `_process_options' (gem) actionpack-3.2.11/lib/action_controller/metal/renderers.rb:34:in `block in _handle_render_options' 

What is entry_url? Why is he looking for him? Do I need to include something in the model. It just has attr_accessors for vars.

 class Entry < ActiveRecord::Base attr_accessible :completed, :endtime, :header, :starttime, :starttimeset, :totaltime, :user, :text, :parent end 

Heres is my routes file:

 Tasks::Application.routes.draw do match '/users/:id/projects' => 'users#show_projects_for_user' authenticated :user do root :to => 'home#index' end root :to => "home#index" devise_for :users resources :users do resources :entries end end 

Thanks for the help.

+7
source share
2 answers

Entry_url is what it asked you to redirect when you say redirect_to @entry

You do not have resource records in the routes file. You have one nested inside the user, but then you need to pass in the same way as the entry.

 redirect_to [ @user, @entry ] 

just saw your comment - if it does it in the JSON way, you should have

 location: [@user, @entry] 

In any case, when you request rails to create the URL for the record, you need to pass the login user, since you have a record nested inside the user in the routes, and not as a separate resource routing.

Adding editing to respond to the comment, as there is no formatting in the comments:

Yes, this will work to remove the location, since it will no longer call the helper to create this location in json, but I assume you want it. So try to do this to get the job done:

 format.json { render json => { :entry => @entry, :status => created, :location => [@user, @entry] }} 

from your comment ... if this doesn’t work then try accessing the URL helper directly

 format.json { render json => { :entry => @entry, :status => created, :location => user_entry_url(@user, @entry) }} 
+9
source

If you use Rails3, this may be because with rails3 the url has become path

Example:

 #rails2 entry_url #rails3 entry_path 

So try entry_path instead of entry_url

0
source

All Articles