0

所以,我是 Rails 新手,我正在构建一个需要版本控制的应用程序。我的问题与这个问题非常相似,但是,尝试给定的解决方案并没有解决我的问题。

这是我得到的错误:

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

基本上,我不知道如何用我正在做的命名空间的东西来解决其他问题。如您所见,我尝试过使用匹配,我已经注释掉了我尝试使用资源和“收集”的地方,但没有骰子。我混合并匹配了各种各样的东西,要么它给了我我在这里描述的错误,要么是“路线不存在”错误。我什至查看了有关 routing 的 rails guides,但无济于事。挣扎了好几天。请帮忙!

4

1 回答 1

0

最终想通了这一点。在我看来,我必须让 form_for 方法调用看起来像这样:

<%= form_for(@question, :url => api_v1_questions_path ) do |f| %>

这解决了我的问题。

于 2013-08-04T16:23:46.577 回答