Rails simple_form_for没有路由匹配,缺少[:id]

时间:2015-02-03 17:48:21

标签: ruby-on-rails simple-form form-for simple-form-for

直截了当,这里有点混乱 这是我的代码

的routes.rb

Rails.application.routes.draw do

  namespace :main, path: ':master_url' do
    root 'sites#index'
    namespace :dashboard do
        root 'dashboards#index'
      resources :masters
    end
    get "/:action" => 'sites#:action'
  end
  root 'main/sites#index'

end

佣金路线

                 Prefix Verb   URI Pattern                                       Controller#Action
                 main_root GET    /:master_url(.:format)                            main/sites#index
       main_dashboard_root GET    /:master_url/dashboard(.:format)                  main/dashboard/dashboards#index
    main_dashboard_masters GET    /:master_url/dashboard/masters(.:format)          main/dashboard/masters#index
                           POST   /:master_url/dashboard/masters(.:format)          main/dashboard/masters#create
 new_main_dashboard_master GET    /:master_url/dashboard/masters/new(.:format)      main/dashboard/masters#new
edit_main_dashboard_master GET    /:master_url/dashboard/masters/:id/edit(.:format) main/dashboard/masters#edit
     main_dashboard_master GET    /:master_url/dashboard/masters/:id(.:format)      main/dashboard/masters#show
                           PATCH  /:master_url/dashboard/masters/:id(.:format)      main/dashboard/masters#update
                           PUT    /:master_url/dashboard/masters/:id(.:format)      main/dashboard/masters#update
                           DELETE /:master_url/dashboard/masters/:id(.:format)      main/dashboard/masters#destroy
                      main GET    /:master_url/:action(.:format)                    main/sites#:action
                      root GET    /                                                 main/sites#index

主要/仪表板/ masters_controller.rb

class Main::Dashboard::MastersController < ApplicationController
before_action :all_masters, only: [:index, :create, :update, :destroy]
before_action :set_master, only: [:edit, :update, :destroy, :show]
before_action :init_master
layout 'main'

respond_to :html, :js

def new
    @master = Master.new
end
def create
    @master = Master.create(conf_params)
end
def edit
end
def update
    @master.update_attributes(conf_params)
end

def destroy
    @master.destroy
end

private
def all_masters
    @masters = Master.all
end
def set_master
    @master = Master.find(params[:id])
end
def conf_params
    params.require(:master).permit(:title,:url)
end
def init_master
    @master_url = Master.find_by_url(params[:master_url])
  end
end

这是我在部分渲染 _form_master.html.erb 时所做的,我调用了 new.js.erb edit.js.erb < /强>

$('#form-master').html("<%= j (render 'form_master', :master => @master) %>");

_form_master.html.erb 内容

  <%= simple_form_for [:main, :dashboard, @master], remote: true do |f| %>
                <%= f.input :url, label: 'URL' %>
                <%= f.input :title, label: 'Title' %>
                <%= f.button :submit %>
            <% end %>

问题:

当我打电话给新主人时,用这个

<%= link_to new_main_dashboard_master_path, remote: true do %>

页面呈现完美无瑕,创造大师做得好 但当我用这行调用编辑时

<%= link_to :controller=>"masters",:action =>"edit",:id => master do %>

which was my problem earlier,rails使用消息返回错误:

ActionView::Template::Error (No route matches {:action=>"show", :controller=>"main/dashboard/masters", :format=>nil, :id=>nil, :master_url=>#<Master id: 4, url: "test", title: "Test Aja", created_at: "2015-02-02 08:55:07", updated_at: "2015-02-02 08:55:07">} missing required keys: [:id]):

我明确地将master.id和master.title的值呈现给页面,返回正确的值。

问:我如何解决这个问题?我尝试了几次,从更改本地传递js.erb,将@model更改为模型,仍然无法正常工作

感谢任何帮助,谢谢! :)

1 个答案:

答案 0 :(得分:0)

这里我们有post = Post.find(params [:post_id])

来自http://blog.8thcolor.com/en/2011/08/nested-resources-with-independent-views-in-ruby-on-rails/

你的模特有没有这样的东西? 你需要一个:某种类型的ID不是吗?

这可能脱离了上下文,但我不会尝试重用您的代码。 尝试类似的东西可能太简单了 render plain:params [:someparam] .inspect 但它对我有帮助。 http://codefol.io/posts/Deep-Rails-Understanding-HashWithIndifferentAccess-Understanding-the-Params-Hash