`../users/1/profile.1` 中的 .1 是什么意思 [英] What does .1 mean in `../users/1/profile.1`

查看:42
本文介绍了`../users/1/profile.1` 中的 .1 是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/../users/1/profile.1 中的 .1 是什么意思?在以一对一的关系编辑关联模型时,例如用户有一个配置文件;它更新并重定向到 ..users/user_id/profile.# 而不是 ../users/user_d/profile.在form_for 中,我使用form_for [:user, @profile] 通过嵌套资源覆盖命名空间,但我不明白为什么.#.为了查看链接是否会导致我的程序中断,我点击了主页(将我带回我的根页面,基本上是重新加载我为登录用户编程的配置文件),它恢复为 ../users/user_d/profile.使用调试 gem 我得到:

--- !ruby/hash:ActionController::Parameters动作:显示控制器:配置文件用户 ID:'1'格式:'1'

什么是格式:'1'?任何解释表示赞赏.

<小时>

添加我的代码

用户.RB

 类用户 

配置文件.RB

类简介

他们的控制器

用户控制器

class UsersController <应用控制器before_action :logged_in_user, only: [:index, :edit, :update, :destroy]before_action :correct_user, only: [:edit, :update]before_action :admin_user, only: :destroy定义新@user = User.new@profile = @user.build_profile结尾定义创建@user = User.new(user_params)如果@user.save登录@用户flash[:success] = "欢迎参加迷你奥运会"重定向到 user_profile_path(current_user,@profile)别的呈现新"结尾结尾高清秀@user = User.find(params[:id])结尾定义编辑# 注释掉代码,因为它是多余的,因为'before_action :correct_user'这行# @user = User.find(params[:id])结尾定义更新# 注释掉代码的第一行,因为'before_action :correct_user' 这行是多余的# @user = User.find(params[:id])如果@user.update_attributes(user_params)flash[:success] = "个人资料更新"#redirect_to @user重定向到 user_profile_path(current_user,@profile)别的渲染编辑"结尾结尾定义索引@users = User.paginate(page: params[:page], per_page: 15)结尾销毁User.find(params[:id]).destroyflash[:success] = "用户已删除"重定向到 users_url结尾私人的定义用户参数params.require(:user).permit(:id, :email, :password, :password_confirmation, profile_attributes: [:name,:street, :city, :state, :zipcode] )结尾# 过滤前# 确认登录用户.定义登录用户除非已登录?商店地址flash[:danger] = "请登录."重定向到 login_url结尾结尾# 确认正确的用户.定义正确_用户@user = User.find(params[:id])redirect_to(root_url) 除非 current_user?(@user) # '@user == current_user' = 'current_user?(@user)'结尾# 确认管理员用户.定义 admin_userredirect_to(root_url) 除非 current_user.admin?结尾结尾

配置文件控制器

class ProfilesController <应用控制器定义编辑@profile = User.find(params[:user_id]).profile结尾高清秀@profile = User.find(params[:user_id]).profile结尾定义更新@profile = User.find(params[:user_id]).profile如果@profile.update_attributes(profile_params)flash[:success] = "个人资料更新"重定向到 user_profile_path(current_user,@profile)别的渲染编辑"结尾结尾私人的def profile_paramsparams.require(:profile).permit(:id,:name,:street,:city,:state,:zipcode)结尾结尾

个人资料编辑表单

<% provide(:title, "Edit Profile") %><h1>更新您的个人资料</h1><div class="row"><div class="col-md-6 col-md-offset-3"><%= form_for [:user, @profile] 做 |f|%><%= 渲染字段",f: f %><%= f.submit "Save changes", class: "btn btn-primary" %><%结束%>

APP/VIEWS/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' %>

路线文件夹

 Rails.application.routes.draw 做根'static_pages#home'得到帮助"=>'static_pages#help'得到关于"=>'static_pages#about'获取联系人"=>'static_pages#contact'获取注册"=>'用户#新'获取登录"=>'会议#新'发布登录"=>'会话#创建'删除注销"=>'会话#销毁'资源:用户做资源:配置文件,仅:[:show, :edit, :update ]结尾结尾

解决方案

例如,通常 url 末尾的点后面的点就是格式.

/users/12.html/用户/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) 配置文件#edituser_profile GET/users/:user_id/profile(.:format) 配置文件#showPATCH/users/:user_id/profile(.:format) 配置文件#updatePUT/users/:user_id/profile(.:format) 配置文件#update

我错过了您使用资源而不是资源的事实,因此每个用户只有一个配置文件.

重定向时使用user_profile_path(@user),不需要传入profile,path中只有一个id,就是user_id.

/What does the .1 mean in ../users/1/profile.1? In editing an associated model in a one to one relationship, such as a user has one profile; it updates and redirected to ..users/user_id/profile.# instead of ../users/user_d/profile. In the form_for, i used form_for [:user, @profile] to cover for the namespace through nested resources, but i don't understand why the .#. In an attempt to see if the link will cause my program to break, i clicked home (to take me back to my root page, basically reloading the profile as i had programmed for a logged in user), it reverts back to ../users/user_d/profile. Using a debugging gem i get:

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

What is format: '1'? Any explanation appreciated.


Adding my Code

USER.RB

  class User < ActiveRecord::Base
    attr_accessor :remember_token

    before_save {self.email = email.downcase }
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[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

Their controllers

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

PROFILE CONTROLLER

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

Profile edit form

<% 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/VIEWS/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 FOLDER

 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

解决方案

Usually the point following a dot at the end of a url is the format, for instance.

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

It looks like you've got a mal-formed url being generated somewhere which is passing the ID in as the format, as well as the id parameter.

That's the explanation, I'm not sure how to get rid of it without a little more information.

  • What does the users and profiles controllers look like in your routes file?
  • What does the link_to or url_for or *_url or *_path which generated this link look like?

Although my best guess is that you could just do form_for(@profile) to tidy this up. Then redirect in your create or update method to users_profiles_path(@user, @profile)

Update:

I put part of your routes file into a new rails app and got these routes

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

I missed the fact that you used resource instead of resources, so that each user has only one profile.

In the redirect, use user_profile_path(@user), you don't need to pass in the profile, the path only has one id in it, and that's the user_id.

这篇关于`../users/1/profile.1` 中的 .1 是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆