2015-08-13 20 views
0

/.1../users/1/profile.1中的含義是什麼?在一對一關係中編輯關聯模型,例如用戶具有一個配置文件;它會更新並重定向到..users/user_id/profile.#而不是../users/user_d/profile。 在form_for中,我用form_for [:user, @profile]來覆蓋命名空間,通過嵌套資源,但我不明白爲什麼.#。在嘗試查看鏈接是否會導致我的程序中斷時,我單擊回家(將我帶回到根頁面,基本上重新加載配置文件,因爲我已爲登錄用戶編寫了該配置文件),它將恢復爲../users/user_d/profile。 使用調試寶石,我得到:「../ users/1/profile.1」中的.1意味着什麼?

--- !ruby/hash:ActionController::Parameters 
    action: show 
    controller: profiles 
    user_id: '1' 
    format: '1' 

什麼是format: '1'?任何解釋讚賞。


添加我的代碼

USER.RB

class User < ActiveRecord::Base 
    attr_accessor :remember_token 

    before_save {self.email = email.downcase } 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i 
    validates :email, presence: true, length: { maximum: 255 }, 
         format:{with: VALID_EMAIL_REGEX}, 
         uniqueness: { case_sensitive: false } 

    has_secure_password 
    validates :password, presence: true, length: { minimum: 6 }, allow_nil: true 

    has_one :profile, dependent: :destroy 

    accepts_nested_attributes_for :profile 

end 

PROFILE.RB

class Profile < ActiveRecord::Base 
    validates :name, presence: true, length: { maximum: 50 } 
    validates :street, :city, :state, :zipcode, presence: true 

    belongs_to :user 
end 

及其控制器

USER CONTROLLER

class UsersController < ApplicationController 
    before_action :logged_in_user, only: [:index, :edit, :update, :destroy] 
    before_action :correct_user, only: [:edit, :update] 
    before_action :admin_user, only: :destroy 

    def new 
    @user = User.new 
    @profile = @user.build_profile 
    end 

    def create 
    @user = User.new(user_params) 
    if @user.save 
    log_in @user 
    flash[:success] = "Welcome to the Mini Olympics" 
    redirect_to user_profile_path(current_user, @profile) 
    else 
    render 'new' 
    end 
end 

def show 
    @user = User.find(params[:id]) 
end 

def edit 
    # Commented out the code, as its redundant due to the line 'before_action :correct_user' 
    # @user = User.find(params[:id]) 
end 

def update 
    # Commented out first line of the code, as its redundant due to the line 'before_action :correct_user' 
    # @user = User.find(params[:id]) 
    if @user.update_attributes(user_params) 
    flash[:success] = "profile updated" 
    #redirect_to @user 
    redirect_to user_profile_path(current_user, @profile) 
    else 
    render 'edit' 
    end 
end 

def index 
    @users = User.paginate(page: params[:page], per_page: 15) 
end 

def destroy 
    User.find(params[:id]).destroy 
    flash[:success] = "User deleted" 
    redirect_to users_url 
end 

private 

    def user_params 
     params.require(:user).permit(:id, :email, :password, :password_confirmation, profile_attributes: [:name, 
     :street, :city, :state, :zipcode]) 
    end 

    # Before filters 

    # Confirms a logged-in user. 
    def logged_in_user 
    unless logged_in? 
     store_location 
     flash[:danger] = "Please log in." 
     redirect_to login_url 
    end 
    end 

    # Confirms the correct user. 
    def correct_user 
    @user = User.find(params[:id]) 
    redirect_to(root_url) unless current_user?(@user) # '@user == current_user' = 'current_user?(@user)' 
    end 

    # Confirms an admin user. 
    def admin_user 
    redirect_to(root_url) unless current_user.admin? 
    end 
end 

簡檔控制器

class ProfilesController < ApplicationController 

    def edit 
    @profile = User.find(params[:user_id]).profile 
    end 

    def show 
    @profile = User.find(params[:user_id]).profile 
    end 

    def update 
    @profile = User.find(params[:user_id]).profile 
    if @profile.update_attributes(profile_params) 
     flash[:success] = "profile updated" 
     redirect_to user_profile_path(current_user, @profile) 
    else 
     render 'edit' 
    end 
    end 

    private 

    def profile_params 
     params.require(:profile).permit(:id, :name, :street, :city, :state, :zipcode) 
    end 

end 

資料編輯形式

<% provide(:title, "Edit Profile") %> 
<h1>Update your profile</h1> 

<div class="row"> 
<div class="col-md-6 col-md-offset-3"> 
    <%= form_for [:user, @profile] do |f| %> 
    <%= render 'fields', f: f %> 
    <%= f.submit "Save changes", class: "btn btn-primary" %> 
    <% end %> 

    </div> 
</div> 

APP /視圖/ PROFILES/_FIELDS.HTML.ERB

<%= f.label :name %> 
<%= f.text_field :name, class: 'form-control' %> 

<%= f.label :street %> 
<%= f.text_field :street, class: 'form-control' %> 

<%= f.label :city %> 
<%= f.text_field :city, class: 'form-control' %> 

<%= f.label :state %> 
<%= f.text_field :state, class: 'form-control' %> 

<%= f.label :zipcode %> 
<%= f.text_field :zipcode, class: 'form-control' %> 

ROUTES文件夾

Rails.application.routes.draw do 

    root    'static_pages#home' 

    get 'help' => 'static_pages#help' 
    get 'about' => 'static_pages#about' 
    get 'contact' => 'static_pages#contact' 

    get 'signup' => 'users#new' 

    get 'login' => 'sessions#new' 
    post 'login' => 'sessions#create' 
    delete 'logout' => 'sessions#destroy' 

    resources :users do 
    resource :profile, only: [:show, :edit, :update ] 
    end 
end 

回答

1

通常,url後面的點後面的點就是格式。

/users/12.html 
/users/12.js 
/users/12/profiles.xml 

看起來你已經有了正在某處被傳遞ID作爲格式,以及作爲ID參數生成一個惡意形成的網址。

這就是解釋,我不知道如何在沒有更多信息的情況下襬脫它。

  • 用戶和配置文件控制器在您的路由文件中看起來像什麼?
  • 生成此鏈接的link_to或url_for或* _url或* _path是什麼樣的?

雖然我最好的猜測是你可以只做form_for(@profile)來整理這件事。然後在您創建或更新方法重定向到users_profiles_path(@user, @profile)

更新:

我把你的路由的部分文件到一個新的Rails應用程序,並得到了這些航線

edit_user_profile GET /users/:user_id/profile/edit(.:format) profiles#edit 
    user_profile GET /users/:user_id/profile(.:format)  profiles#show 
        PATCH /users/:user_id/profile(.:format)  profiles#update 
        PUT /users/:user_id/profile(.:format)  profiles#update 

我錯過了一個事實,即您使用資源而不是資源,以便每個用戶只有一個配置文件。

在重定向中,使用user_profile_path(@user),您不需要傳入配置文件,路徑中只有一個id,這就是user_id。

+0

我剛剛添加了我的代碼,以代替更好的理解。 –

+0

@OpyOsegs我想我已經找到它了,試試'user_profile_path(@user)' – AJFaraday

+0

哦,哇,我現在也看到了,謝謝。 –

0

路徑末尾的「點東西」表示您想要獲得的格式。 所以,如果你鍵入profile.json,rails會知道你想要json的答案,並將相應地在控制器中呈現(如果這個支持)。例如,

+0

謝謝,但爲什麼然後生成的id爲格式? –

+0

不知道但值得一試:通過redirect_to user_profile_path(user_id:current_user.id,id:@ profile.id)替換redirect_to user_profile_path(current_user,@profile) – Nicolas

+0

Did not; t work,just throw the error'Could not find User with 'id'= user_id'用於我的演出動作。即使'redirect_to user_profile_path(current_user.id,id:@ profile.id)'只是做了原始錯誤。 –

相關問題