gpt4 book ai didi

sql - Ruby NoMethodError - BlahController 的未定义方法 `blah_url'

转载 作者:数据小太阳 更新时间:2023-10-29 06:58:49 25 4
gpt4 key购买 nike

我从一个链接调用这个 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
});

}

它击中了这个 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

这是服务器上的响应:

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 是它要求您重定向到的内容

您在路由文件中没有条目资源。您确实有一个嵌套在用户中,但是您需要传递以及输入。

redirect_to [ @user, @entry ]

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

location: [@user, @entry]

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

添加编辑以响应评论,因为评论中没有格式:

是的,这将用于删除位置,因为它将不再调用帮助程序在 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) }}

关于sql - Ruby NoMethodError - BlahController 的未定义方法 `blah_url',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15629741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com