7

私はリンクからこの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

助けてくれてありがとう。

4

2 に答える 2

9

entry_urlは、redirect_to@entryと言ったときにリダイレクトするように求めているものです。

ルートファイルにエントリリソースがありません。ユーザー内にネストされたものがありますが、エントリと同様に渡す必要があります。

redirect_to [ @user, @entry ]

コメントを見ただけです-JSONパスでこれを行っている場合は、同様に

location: [@user, @entry]

基本的に、レールにエントリの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) }}
于 2013-03-26T05:13:36.263 に答える
0

Rails3を使用している場合、これは、rails3ではURLがパスになっているために発生する可能性があります

元:

#rails2
entry_url

#rails3
entry_path

だからentry_path代わりに試してみてくださいentry_url

于 2013-03-26T06:06:11.327 に答える