用户登录成功后页面链接
<li><%= link_to "信息修改", settings_user_path(current_user) %></li>
来修改用户信息。
route 里面这样定义:
# 用户
resources :users do
member do
get :settings
end
end
执行 bundle exec rake routes 后结果如下:
settings_user GET /users/:id/settings(.:format) users#settings
POST /users/:id/settings(.:format) users#settings
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
views/user/settings.html.erb里面核心代码截取如下:
<div style="display:block " id="basic" class="column two-thirds">
<%= form_for(@user, url:settings_user_path(params[:id])) do |f| %>
<%=render 'shared/error_messages', object: f.object %>
<%= f.label :gender, '性别' %>
<%= f.select :gender, ['female','male','others'] %>
<%= f.label :age %>
<%= f.text_field :age %>
<%= f.label :height, '身高(如:175)' %>
<%= f.text_field :height %>
<%= f.label :hometown %>
<%= f.text_field :hometown %>
<%= f.label :constellation, '星座' %>
<%= f.text_field :constellation %>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
</div>
users_controller 里面 settings 方法是这样的:
def settings
@user = User.find_by_id(params[:id])
if @user.update_attributes(basic_params)
flash[:success] = "基本信息更新成功"
elsif @user.update_attributes(accounts_params)
flash[:success] = "账户信息更新成功"
elsif @user.update_attributes(loved_params)
flash[:success] = "恋爱信息更新成功"
elsif @user.update_attributes(biography_params)
flash[:success] = "背景信息更新成功"
else
render 'root'
end
end
users_controller 里面参数定义截取如下:
private
def basic_params
params.require(:user).permit(:gender, :age, :height, :hometown, :constellation)
end
但我点击链接
<li><%= link_to "信息修改", settings_user_path(current_user) %></li>
后报错:param is missing or the value is empty: user,如果 settings 方法体删除掉就不会报这个错。 参考 stack overflow 一篇文章的意思是我的 settings action 不起作用:http://stackoverflow.com/questions/31299920/rails-param-is-missing-or-the-value-is-empty-user 可是我还是不理解是哪里的错误。