嵌套路由没有路由匹配发布但路由存在于Rake路由中

时间:2014-07-18 14:39:33

标签: ruby-on-rails routes

更新

Started GET "/classifieds/12/questions/new" for 127.0.0.1 at 2014-07-18 10:55:27 -0400
Processing by QuestionsController#new as HTML
  Parameters: {"classified_id"=>"12"}
  Classified Load (0.5ms)  SELECT "classifieds".* FROM "classifieds" WHERE "classifieds"."id" = $1 LIMIT 1  [["id", "12"]]
  Rendered questions/new.html.erb within layouts/application (2.5ms)
Completed 500 Internal Server Error in 35ms

TypeError - can't convert Symbol into Integer:

我有一个belongs_to :classified的问题模型和has_many :questions

的分类模型

因此这种关系,我在routes.rb中做了一个嵌套路由

  resources :classifieds do 
    resources :questions
  end

由于上述情况,我在获取路线时获得了这些新路线

 classified_questions GET    /classifieds/:classified_id/questions(.:format)                               questions#index
                                                        POST   /classifieds/:classified_id/questions(.:format)                               questions#create
                                new_classified_question GET    /classifieds/:classified_id/questions/new(.:format)                           questions#new
                               edit_classified_question GET    /classifieds/:classified_id/questions/:id/edit(.:format)                      questions#edit
                                    classified_question GET    /classifieds/:classified_id/questions/:id(.:format)                           questions#show
                                                        PUT    /classifieds/:classified_id/questions/:id(.:format)                           questions#update
                                                        DELETE /classifieds/:classified_id/questions/:id(.:format)                           questions#destroy

我正在挖掘指南中的嵌套资源部分。新问题应该给我一个表格,我可以发布问题#create,允许我将问题发布到属于特定分类广告的数据库。

在我的架构中的问题表中,我添加了这个挂钩

t.integer  "classifieds_id"

****我没有分类控制器。

这是我的渲染show方法的控制器

class UplatzPlacesController < ApplicationController
  require 'balanced'
    helper_method :countries
  def show
    @classified=Classified.find(params[:id])
  end
end

show.html.erb向用户显示分类广告,底部显示link_to

 <%= link_to "Post a question", new_classified_question_path(@classified.id) %>

我生成了自己的问题控制器

class QuestionsController < ApplicationController

  def index
  end

  def new
    @classified = Classified.find(params[:classified_id])
    @question = Question.new 
  end 

  def create
    @classified = Classified.find(params[:classified_id])
    @question = Question.new(params[:question])
    @classified.questions << @question
    if @question.save 
      flash[:notice] = "Question has been posted"
    else
      flash[:notice] = "It did not go through"
    end
  end
end

当我点击link_to时,我确实看到了表格,而网址就变成了这个

classifieds/12/questions/new #shows that id got passed correctly

但是当我点击提交时,我收到此路由错误

Routing Error

No route matches [POST] "/classifieds/12/questions/new"

***每条路线确实存在帖子。

这是我在视图/问题中的new.html.erb

<%=form_for @question, :url => new_classified_question_path do |f| %>
      <fieldset>
        <ul id='posting_question_form'>
          <li>
            <%=f.label :question_body, 'Post Your Question To Seller:'%>
            <%= f.text_area :question_body, :cols=>40, :rows=>50 %>
          </li>
        </ul>
      </fieldset>
      <div>
      <%= f.submit "Post Question", :class => 'goButton', :style => 'width:auto;float:right;' %>
      </div>
  <% end %>
</div>

我确实在问题模型中设置了质量分配的属性

attr_accessible :question_body, :classifieds_id

它应该有效,但它不是。

该应用也是gem 'rails', '3.2.13'

2 个答案:

答案 0 :(得分:1)

您使用的是错误的网址帮助:

new_classified_question_path

此助手会导致嵌套在new中的控制器questions的{​​{1}}操作。 (Rails中的URL Helper的结构如下:classified或以<action>_<parent_controller>_<controller_name>_path而不是_url结尾。)

您应该使用:

_path

您的指南:http://guides.rubyonrails.org/form_helpers.html

答案 1 :(得分:0)

在新控制器上,应该构建一个新问题

# GET /classifieds/:classified_id/questions/new
def new
  # 1st you retrieve the classified to params[:classified_id]
  @classified = Classified.find(params[:classified_id])     
  # 2nd you build a new one of question
  @question = @classified.questions.build
end

# POST /classifieds/:classified_id/questions
def create
  # 1st you retrieve the classified to params[:classified_id]
  @classified = Classified.find(params[:classified_id])
  #2nd you create the question with arguments in params[:question]
  @question = @classified.questions.create(params[:question])
  respond_to do |format|
   if @question.save 
     # redirection to the Question resource
     format.html { redirect_to([@question.classified, @question], :notice => 'Question has been posted.')
     # or redirection to the list of Questions
     # redirect_to(classified_@questions_url)
   else
     format.html { 
       flash[:error] = "It did not go through"
       render :action => "new" 
     } 
   end
  end
end

并在新表格上

<%= form_for([@question.classified, @question]) do |f| %>
   ....
<% end %>

创建嵌套资源的另一种方法:Creating nested resources in ruby on rails 3 and updating scaffolding links and redirection