【问题标题】:Nested Routes No Route Matches Post But Route Exist in Rake Routes嵌套路由没有路由匹配发布但路由存在于 Rake 路由中
【发布时间】:2014-07-18 14:39:33
【问题描述】:

更新

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

由于上述原因,当我执行 rake 路线时,我得到了这些新路线

 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

我正在浏览指南中的嵌套资源部分。新问题应该给我一个表格,我可以在其中发布到 questions#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 时,我确实看到了表单,并且 url 变成了这个

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'

【问题讨论】:

    标签: ruby-on-rails routes


    【解决方案1】:

    您使用了错误的 URL Helper:

    new_classified_question_path
    

    这个助手导致控制器questions嵌套在classified中的new动作。 (Rails 中的 URL Helper 的结构如下:&lt;action&gt;_&lt;parent_controller&gt;_&lt;controller_name&gt;_path 或以_url 结尾而不是_path)。

    你应该使用:

    form_for @question, classified_questions_path(@classified) do |f|
    

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

    【讨论】:

    • @vee 感谢复数语法错误。关于method: :post,我不确定。我认为form_for 的默认方法是:get,现在去检查一下
    • @vee 我的错,默认不是GET,谢谢你的评论!
    • 似乎它的发布正确,但现在我收到类型错误。更改此帮助程序后,我是否必须更改控制器方法中的任何内容?
    • 错误信息是什么?以及涉及的线路是什么?
    • 这实际上是你给我的form_for。实际上,在我的 link_to 中,我只是传入 @classified.id 而不是 @classified。 nvm 更改 @classified.id 无效。
    【解决方案2】:

    在你的新控制器上,应该建立一个新问题

    # 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

    【讨论】:

      猜你喜欢
      • 2014-09-27
      • 2016-12-08
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多