2013-02-27 53 views
0

我是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这使/用户/用户ID,这里改名但正如上面我是有它的问题指出,所以我改变文件名):

<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' 
+0

你能找出导致错误的线吗? – jvnill 2013-02-27 14:56:55

+0

我看到配置文件和更新方法都是一样的。为什么不使用相同的方法? – 2013-02-27 14:57:32

回答

0

你应该把在routes.rb中

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

虽然,我不会用改变show.html到profile.html的这种做法,但这应该在固定的路线:)

+0

你不应该使用'match',你应该对你期望的请求使用正确的动词(在这种情况下,'get')。 – sevenseacat 2013-02-27 14:50:13

+0

糟糕的情况下,没有好的和正确的选择。我不会改变表演,如果你问我:)但好的,谢谢你的评论。得到也可以使用。 – Aleks 2013-02-27 14:52:45

+0

这工作。谢谢 – 2013-02-27 15:09:39

0

变化

get 'profile' => 'users#profile' 

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

或一个更好的解决办法是增加一个成员的路线下的用户

resources :users do 
    get :profile, on: :member 
end 
+0

它提供了相同的错误。还有其他建议吗? – 2013-02-27 14:49:26

+0

更新了我的回答 – jvnill 2013-02-27 14:50:00

+0

是的,那是在更新后的选项,因为我都尝试过。不幸的是仍然产生相同的错 – 2013-02-27 14:52:41

0

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

<%= form_for(@user, :url => {:action => :profile}) do |f| %>