Ruby NoMethodError - undefined method `blah_url' for BlahController

余生长醉 提交于 2020-01-03 08:55:07

问题


I am calling this js from a 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
});

}

It hits 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 the entry_url? Why is it looking for it? Do i need to include something in the model. Its just has attr_accessors for the 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.


回答1:


The entry_url is what it's asking you to redirect to when you say redirect_to @entry

You don't have an entries resource in the routes file. You do have one nested within user, but then you need to pass as well as the entry.

redirect_to [ @user, @entry ]

just saw your comment - if it's doing this on the JSON path similarly you need to have

location: [@user, @entry]

Basically anywhere you're asking rails to build a url for an entry you need to pass the entry's user in because you have entry nested within user in the routes and not as a standalone resource routing.

Adding an edit to respond to the comment because there's no formatting in comments:

Yes, this it will work to delete the location as it will no longer call the helper to build that location in the json, but I am presuming you want that. So try this to make the location work:

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

from your comment... if that's not working then let's try calling the url helper directly

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



回答2:


If you are using Rails3, this might case because with rails3, the url has become path

Ex:

#rails2
entry_url

#rails3
entry_path

So try entry_path instead of entry_url



来源:https://stackoverflow.com/questions/15629741/ruby-nomethoderror-undefined-method-blah-url-for-blahcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!