[英]routing error for profile page
I am new to Rails and I have at work setting up the user profile and adding fields to it for users to add details to their profile. 我是Rails的新手,我正在工作中设置用户个人资料并向其中添加字段,以便用户向其个人资料中添加详细信息。 This information is completely different from the registration options (account settings). 此信息与注册选项(帐户设置)完全不同。 The issue I had was the submit button on the profile page is only recogonizing it as the submit button from the registration page. 我遇到的问题是个人资料页面上的“提交”按钮,只是将其重新显示为注册页面上的“提交”按钮。 So when users select all their profile options (career, religion, height, etc) and then click submit it redirect them to the registration page and never saved the profile options to the user. 因此,当用户选择所有个人资料选项(职业,宗教信仰,身高等),然后单击“提交”时,会将他们重定向到注册页面,并且永远不会将个人资料选项保存给用户。
I believe I fixed that problem and I now have a routing error "No route matches [GET] "/profile/4"" 我相信我已经解决了该问题,现在出现了路由错误“没有路由匹配[GET]“ / profile / 4””
Users_controller: Users_controller:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
UserMailer.registration_confirmation(@user).deliver
session[:user_id] = @user.id
redirect_to root_url, notice: "Thank you for signing up!"
else
render "new"
end
end
def profile
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
flash[:success] = "Account updated"
redirect_to @user
else
render 'edit'
end
end
def edit
@user = User.find(params[:id])
end
def index
@users = User.all
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted."
redirect_to users_url
end
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
flash[:success] = "Account updated"
redirect_to @user
else
render 'edit'
end
end
end
Here's the profile.html (renamed from show.html which allowed /users/user-id-here but as noted above I was having issues with it so I changed file name): 这是profile.html(从show.html重命名,它允许在/ users / user-id-here,但是如上所述,我遇到了问题,因此我更改了文件名):
<h1><%= @user.username %></h1>
<h2>Basics</h2>
<%= form_for @user do |f| %>
<div class="field">
<%= f.label :height %><br/>
<%= f.select :about_me, [['Feet', nil], '4', '5', '6'] %>
<%= f.select :about_me, [['Inches', nil], '0', '1', '2', '3', '4',
'5', '6', '7', '8', '9', '10', '11'] %>
</div>
<div class="field">
<%= f.label :children %><br/>
<%= f.select :children, [['Do you have or want kids?', nil], 'Yes, they live with me', 'I want kids now', 'I want one someday', 'Not for me']%>
</div>
<div class="field">
<%= f.label :religion %><br/>
<%= f.select :religion, [['What is your faith?', nil], 'Agnostic', 'Atheist', 'Christian', 'Catholic', 'Buddhist', 'Hindu', 'Jewish', 'Muslim', 'Spiritual without affiliation', 'Other', 'None', 'Prefer not to say' ]%><br/>
<%= f.select :religion, [['How important is this to you?', nil], 'Very Important', 'Somewhat Important', 'Not Important']%>
</div>
<div class="field">
<%= f.label :career %><br/>
<%= f.text_field :career %>
</div>
<div class="field">
<%= f.label :education %><br/>
<%= f.select :education, [['What is your education level?', nil], 'High school', 'Some college', 'Undergraduate', "Bachelor's", "Master's ", 'PhD', 'Business school', 'Law school', 'Medical school' ]%>
</div>
<div class="field">
<%= f.label :ethnicity %><br/>
<%= f.select :ethnicity, [['What is your ethnicity?', nil], 'Asian', 'Black', 'Biracial', 'Indian', 'Hispanic/Latin', 'Middle Eastern', 'Native American', 'Pacific Islander', 'White', 'Other' ]%>
</div>
<%= f.label :user_drink %><br/>
<%= f.select :user_drink, [['How much do you drink?', nil], 'Often Drinks', 'Sometimes drinks', 'Never drinks', 'No comment' ]%>
</div><br/>
<%= f.label :user_smoke %><br/>
<%= f.select :user_smoke, [['How often do you smoke?', nil], 'Often smokes', 'Sometimes smokes', 'Never smokes'] %>
</div>
<div class="actions"><%= f.submit %></div>
<h3>About Me</h3>
<%= form_for @user do |f| %>
<div class="field">
<%= f.label :about_me %><br/>
<%= f.text_field :about_me %>
<div class="actions"><%= f.submit %></div>
<% end %>
<% end %>
Here's routes file: 这是路线文件:
Dating::Application.routes.draw do
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
get 'logout' => 'sessions#destroy'
get 'edit' => 'users#edit'
get 'profile' => 'users#profile'
resources :users
resources :sessions
resources :password_resets
root to: 'users#new'
change 更改
get 'profile' => 'users#profile'
to 至
get 'profile/:id' => 'users#profile'
or a much better solution is to add a member route under users 或更好的解决方案是在用户下添加成员路由
resources :users do
get :profile, on: :member
end
you should put in routes.rb 你应该把routes.rb
match "/profile/:id" => "users#show"
Although ,I wouldn't use that approach of changing show.html to profile.html, but this should work in fixing routes :) 虽然,我不会使用将show.html更改为profile.html的方法,但这应该可以解决路由问题:)
您需要在form_for中将路径明确定义为:
<%= form_for(@user, :url => {:action => :profile}) do |f| %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.