0

我是 Rails 的新手,我正在设置用户个人资料并向其中添加字段,以便用户向他们的个人资料添加详细信息。此信息与注册选项(帐户设置)完全不同。我遇到的问题是个人资料页面上的提交按钮仅将其识别为注册页面上的提交按钮。因此,当用户选择他们所有的个人资料选项(职业、宗教、身高等)然后单击提交时,会将他们重定向到注册页面,并且永远不会将个人资料选项保存给用户。

我相信我解决了这个问题,现在我遇到了路由错误“没有路由匹配 [GET]“/profile/4””

用户控制器:

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(从允许 /users/user-id-here 的 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'
4

3 回答 3

0

您需要在 form_for 中明确定义您的路径:

<%= form_for(@user, :url => {:action => :profile}) do |f| %>
于 2013-02-27T15:06:26.013 回答
0

改变

get 'profile' => 'users#profile'

get 'profile/:id' => 'users#profile'

或者更好的解决方案是在用户下添加成员路由

resources :users do
  get :profile, on: :member
end
于 2013-02-27T14:48:33.657 回答
0

你应该把 routes.rb

match "/profile/:id" => "users#show"

虽然,我不会使用将 show.html 更改为 profile.html 的方法,但这应该可以修复路由:)

于 2013-02-27T14:49:45.020 回答