我是rails的新手。我在尝试实现创建操作时遇到以下错误。
Routing Error
No route matches {:action=>"show", :controller=>"settings", :format=>nil}
我的控制器文件如下: -
@settings = Setting.new(params[:settings])
respond_to do |format|
if @settings.save
format.html { redirect_to @settings, notice: 'Setting was successfully created.' }
format.json { render json: @settings, status: :created, location: @settings }
else
format.html { render action: "new" }
format.json { render json: @settings.errors, status: :unprocessable_entity }
end
end
端
我的Routes.rb文件如下: -
resources :settings do
member do
post 'add'
post 'remove'
get 'settings/id'
end
collection do
get 'add'
get 'list'
post 'get_settings'
get 'get_settings'
end
end
resources :settings
我的佣金路线有以下几种: -
GET /settings/get_settings(.:format
settings#get_settings
GET /settings(.:format)
settings#index
POST /settings(.:format)
settings#create
GET /settings/new(.:format)
settings#new
GET /settings/:id/edit(.:format)
settings#edit
GET /settings/:id(.:format)
settings#show
PUT /settings/:id(.:format)
settings#update
DELETE /settings/:id(.:format)
settings#destroy
GET /settings(.:format)
settings#index
POST /settings(.:format)
settings#create
GET /settings/new(.:format)
settings#new
GET /settings/:id/edit(.:format)
settings#edit
GET /settings/:id(.:format)
settings#show
PUT /settings/:id(.:format)
settings#update
DELETE /settings/:id(.:format)
settings#destroy
我的create.html.erb如下:_
<%= form_for @setting do |f| %>
<% if @setting.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@setting.errors.count, "error") %> prohibited this setting from being saved:</h2>
<ul>
<% @setting.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
</br></br>
Id: <%= f.text_field :id %><br>
Name: <%= f.text_field :name %><br>
<%= f.submit "Create" %>
我的错误日志是: -
Started GET "/settings/new" for 127.0.0.1 at 2013-03-12 18:57:09 +0530
Processing by SettingsController#new as HTML
Rendered settings/new.html.erb within layouts/application (170.2ms)
Completed 500 Internal Server Error in 1112ms
ActionController::RoutingError (No route matches {:action=>"show", :controller=>
"settings", :format=>nil}):
app/views/settings/new.html.erb:7:in `_app_views_settings_new_html_erb__979995
802_23360592'
app/controllers/settings_controller.rb:29:in `new'
Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.1
1/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within resc
ues/layout (0.0ms)
我的new.html: -
<h1>New setting</h1>
<%= form_for @setting do |f| %>
<% if @setting.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@setting.errors.count, "error") %> prohibited this setting from being saved:</h2>
<ul>
<% @setting.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
</br></br>
Id: <%= f.text_field :id %><br>
First Name: <%= f.text_field :name %><br>
<%= f.submit "Create" %>
<% end %>
<%= link_to 'Back', settings_path %>
任何人都可以帮助我。
答案 0 :(得分:0)
一个好的起点始终是通过以下方式打印出您的路线:
rake routes
然后你需要在控制器中定义一个show方法:
def show
..
end
观点:
app/views/settings/show.html.erb
使用resources :settings
时,他应该认识到有一个显示路径。女巫看起来像这样:
settings_path(user.id)
resources :settings
不需要包含两次。
它与你的UserController
无关。 importend部分是SettingController
。如果您在/view/user/show.html.erb
上显示SettingsShow链接,则可能会出现错误? (不是100%肯定,需要查看更多代码,SettingController
将是非常有用的。)
答案 1 :(得分:0)
将表单放在new.html.erb
而不是create.html.erb
中,如:
<强> new.html.erb 强>
<%= form_for @setting do |f| %>
#Your stuff
<% end %>
然后你的新行动是:
新动作
def new
@setting = Setting.new
end
你的控制器就是 -
制作行动:
更新:
def create
if request.setting?
@setting = Setting.new(params[:setting])
# other setup for save
if @setting.save
flash[:notice] = 'Setting was successfully created.'
redirect_to @setting
else
render :action => 'new'
end
end
end
注意:当你说create是post方法时,没有create.html.erb
这样的东西。另外,您应该在代码中使用@setting
而不是@settings
,就像在新的控制器方法中定义的那样。