【发布时间】:2014-04-06 05:28:16
【问题描述】:
我试图在我的应用程序中为注册用户创建更新表单
查看:
<h2><%= @title =%></h2>
<% if @user.errors.any? %>
<div id="error">
<h2><%= pluralize(@user.errors.count,"error") %> prohibited this user from registering</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg%></li>
<%end%>
</ul>
</div>
<%end%>
<%= form_for :user do |form| %>
<div class="reg_form">
<label for="first_name">First Name: </label>
<%= form.text_field :first_name %>
</div>
<div class="reg_form">
<label for = "last_name">Last Name: </label>
<%= form.text_field :last_name %>
</div>
<div class="reg_form">
<%=form.submit "Update"%>
</div>
<%end%>
控制器:users_controller.rb
def index
unless session[:user_id]
flash[:notice] = "Please Login first"
redirect_to :action => "new"
end
@title = "Profile"
@user=User.find(session[:user_id])
end
def edit
@title="Profile Update"
@user = User.find(session[:user_id])
if param_posted?(:user)
if @user.update_attributes(params[:user])
flash[:notice] = "Update Successful"
redirect_to :controller => "user", :action => "profile"
end
end
end
我在 routes.rb 中包含了 get "edit"=> 'users#edit'。但是,我收到一个错误 '没有路线匹配 [POST] "/users/edit"'
感谢任何帮助。谢谢。
【问题讨论】:
-
而不是使用这个 ,尝试替换为
-
@Lalitharyani 所以控制器没有其他变化?
-
无需在您的路由文件中明确定义编辑路由...只需添加资源 :users ,它将为您的编辑操作创建默认路由
-
@Lalitharyani 资源 :users 已包含在 routes.rb 中,更改为 '@user' 并没有帮助。还有其他建议吗?
-
尝试从您的控制台运行 rake 路由并删除您已明确定义的编辑路由。
标签: ruby-on-rails ruby rails-routing