所以,我是rails的新手,我正在构建一个需要版本化的应用程序。我的问题与this question非常相似,但尝试给定的解决方案并未解决我的问题。
这是我得到的错误:
ActionView::Template::Error (undefined method `questions_path' for #<#<Class:0x000000057bd2c0>:0x000000057b00e8>)
这是我的观点:
<h1>Create Question </h1>
<%= form_for(@question) do |f| %>
<div>
<%= f.label :text %>
<%= f.text_field :text %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
这是我的控制器:
class Api::V1::QuestionsController < Api::V1::ApiController
inherit_resources
load_and_authorize_resource :user
load_and_authorize_resource :through => :user
def index
respond_with @questions.unanswered(current_user)
end
def new
@question = Question.new
end
def create
@question = Question.new(params[:question])
if @question.save
#render :action => 'new'
else
puts @question.text
puts @question.level
puts @question.answers.inspect
end
end
#other methods...
end
最后,我的路线的当前版本:
namespace :api, :defaults => { :format => 'json' } do
namespace :v1 do
resources :users do
resources :questions, :path => 'questions' do
collection do
#get 'new', :to => 'questions#new', :as => :new_api_v1_user_question_path
end
end
end
resources :questions, :path => 'questions' do
resources :answers do
resources :responses
end
collection do
#get 'new', :to => 'questions#new', :as => :new_api_v1_question_path
end
end
end
end
match 'api/v1/user/:user_id/questions/new.html', :to => 'api/v1/user/:user_id/questions#new', :as => :new_api_v1_user_question
基本上,我不知道如何使用我正在做的命名空间来完成that other question的解决方案。正如你所看到的,我已经尝试过使用匹配,我已经注释了我尝试使用资源和'集合'的地方,但没有骰子。我混合并匹配各种各样的东西,要么它给我我在这里描述的错误,或者“路线不存在”错误。我甚至看过rails guides on routing,但无济于事。挣扎了几天。请帮忙!
答案 0 :(得分:0)
最终想出来了。在我看来,我必须使form_for方法调用看起来像这样:
<%= form_for(@question, :url => api_v1_questions_path ) do |f| %>
这解决了我的问题。