Ruby NoMethodErroR语言 BlahController 的未定义方法 'blah_url'



我从一个链接调用这个js:

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
});
}

它击中了这个控制器:

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

服务器上的响应:

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'

entry_url是什么?它为什么要找它?我是否需要在模型中包含一些东西。它只是为变量设置了attr_accessors。

class Entry < ActiveRecord::Base
  attr_accessible :completed, :endtime, :header, :starttime, :starttimeset, :totaltime, :user, :text, :parent
end
这是我的路由文件:
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

谢谢你的帮助

输入redirect_to @entry时要重定向到的是entry_url

你的路由文件中没有条目资源。您确实在user中嵌套了一个,但是您需要传递条目和条目。

redirect_to [ @user, @entry ]

刚刚看到你的评论-如果它在JSON路径上做同样的事情,你需要有

location: [@user, @entry]

基本上在任何你要求rails为一个条目构建url的地方,你都需要传递条目的用户,因为你在路由中嵌套了条目在用户中,而不是作为一个独立的资源路由。

添加一个编辑来回应注释,因为注释中没有格式:

是的,这将工作来删除位置,因为它将不再调用helper来构建json中的位置,但我假设你想要的。所以试着这样做,使位置工作:

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

从你的评论…如果不行,我们试试直接调用url帮助器

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

如果您使用的是Rails3,可能会出现这种情况,因为在Rails3中,url已经变成了path

,

#rails2
entry_url
#rails3
entry_path

entry_path代替entry_url

最新更新