Я новичок в Rails, и у меня есть на работе настройка профиля пользователя и добавление к нему полей, чтобы пользователи могли добавлять детали в свой профиль. Эта информация полностью отличается от параметров регистрации (настроек учетной записи). Проблема, с которой я столкнулась, - кнопка отправки на странице профиля, только переучивает ее как кнопку отправки на странице регистрации. Поэтому, когда пользователи выбирают все свои параметры профиля (карьера, религия, высота и т. Д.), А затем нажимают кнопку «Отправить», перенаправляют их на страницу регистрации и никогда не сохраняют параметры профиля для пользователя.Ошибка маршрутизации для страницы профиля
Я считаю, что я установил эту проблему, и теперь у меня есть ошибки маршрутизации "нет маршрута матчей [не GET] "/ профиль/4""
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
Вот profile.html (переименованы из show.html, что позволило/пользователи/идентификатор пользователя-здесь, но, как отмечалось выше, у меня были проблемы с ним, поэтому я изменил имя файла):
<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 %>
Вот маршруты файла:
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'
Вы можете определить линию, вызывающую ошибку? – jvnill
Я вижу, что оба метода профиля и обновления одинаковы. почему не использовать тот же метод? –