Ruby NoMethodError-BlahController的未定义方法'blah_url' [英] Ruby NoMethodError - undefined method `blah_url' for BlahController

查看:82
本文介绍了Ruby NoMethodError-BlahController的未定义方法'blah_url'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从链接中调用此js:

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

}

它击中了这个控制器:

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是什么?为什么要寻找它?我需要在模型中包括一些东西吗?它只是有vars的attr_accessors.

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

这是我的路线文件:

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

感谢您的帮助.

推荐答案

entry_url是当您说redirect_to @entry时要求您重定向到的内容

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

在routes文件中没有条目资源.您确实在用户内嵌套了一个,但是随后您还需要传递条目.

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 ]

刚刚看到了您的评论-如果要在JSON路径上同样执行此操作,则需要

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

location: [@user, @entry]

基本上在您要求Rails为条目建立URL的任何地方,您都需要传递该条目的用户,因为您将条目嵌套在路由中的用户内,而不是作为独立的资源路由.

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:

是的,这将删除该位置,因为它将不再调用帮助程序以在json中建立该位置,但是我想您想要的是.因此,请尝试执行以下操作以使位置正常运行:

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] }}

从您的评论中...如果不起作用,让我们尝试直接调用url帮助器

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

这篇关于Ruby NoMethodError-BlahController的未定义方法'blah_url'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆