[英]Routing error while implementing create in rails 3.2
I am new to rails.I am getting the following error while trying to implement a create action.. 我是Rails的新手,在尝试实施create动作时遇到以下错误。
Routing Error
No route matches {:action=>"show", :controller=>"settings", :format=>nil}
My controller file is as follows:- 我的控制器文件如下:-
@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
end 结束
My Routes.rb file is as follows:- 我的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
my rake routes has the following:- 我的耙路如下:
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
My create.html.erb is as follows:_ 我的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" %>
My error log is:- 我的错误日志是:-
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)
My new.html:- 我的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 %>
Can anyone please help me. 谁能帮帮我吗。
A good start point is always to print out your routes via: 一个好的起点始终是通过以下方式打印出您的路线:
rake routes
Then you need to define a show method in the controller: 然后,您需要在控制器中定义show方法:
def show
..
end
And a view: 和一个视图:
app/views/settings/show.html.erb
With the resources :settings
he should recognize that there is a show path. 借助
resources :settings
他应该认识到存在展示路径。 Witch could look like this: 女巫看起来像这样:
settings_path(user.id)
resources :settings
dont needs to be included twice. resources :settings
不需要被包含两次。
And it has nothing to do with your UserController
i guess. 我猜这与您的
UserController
无关。 The importend part is the SettingController
. importend部分是
SettingController
。 The error could appear if you display a SettingsShow link on the /view/user/show.html.erb
? 如果在
/view/user/show.html.erb
上显示SettingsShow链接,则可能会出现错误。 (Not 100% sure, need to see more code, SettingController
would be interessting). (不是100%肯定,需要查看更多代码,
SettingController
会引起SettingController
)。
Put your form in new.html.erb
instead of create.html.erb
like: 将表单放在
new.html.erb
而不是像create.html.erb
那样:
new.html.erb new.html.erb
<%= form_for @setting do |f| %>
#Your stuff
<% end %>
Your new action then be: 然后,您的新操作是:
new action 新动作
def new
@setting = Setting.new
end
Your controller then be- 然后,您的控制器将-
create action: 创建动作:
UPDATE: 更新:
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
NOTE: There is no such thing as create.html.erb
when you say create is a post method. 注意:当您说create是post方法时,没有诸如
create.html.erb
这样的东西。 Also you should use @setting
throughout your code instead of @settings
as what it defined in a new method of controller. 同样,您应该在整个代码中使用
@setting
,而不是在新的控制器方法中定义的@settings
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.