【发布时间】:2015-02-03 17:48:20
【问题描述】:
直截了当,这里有点混乱 这是我的代码
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
rake 路线
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
main/dashboard/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 更改为只是模型,仍然无法正常工作
感谢任何帮助,谢谢! :)
【问题讨论】:
-
我们不用通过
:id => master.id -
一般情况下:
<%= link_to edit_main_dashboard_master(master) do %> -
^ 你缺少 _path ;)
edit_main_dashboard_master_path(master) -
使用
edit_main_dashboard_master_path(master)调用编辑来渲染索引,其中包含上面行的link_to,错误:ActionView::Template::Error (No route matches {:action=>"edit", :controller=>"main/dashboard/masters", :format=>nil, :id=>nil, :master_url=>#<Master id: 4, url: "aja", title: "Test Aja", created_at: "2015-02-02 08:55:07", updated_at: "2015-02-02 08:55:07">} missing required keys: [:id]):,这是我之前的问题in this thread -
所以我想出了改变link_to的解决方案:
<%= link_to :controller=>"masters",:action =>"edit",:id => master do %>或link_to edit_main_dashboard_master_path(@master_url.url, master), remote: true do这两行返回正确的路径,但打破了简单的形式:|你知道我有点困惑@Sontya @ptd @H-man 反正谢谢伙计
标签: ruby-on-rails simple-form form-for simple-form-for