什么.1指的是`./users/1/profile.1`

时间:2015-08-13 17:22:03

标签: ruby-on-rails ruby-on-rails-4

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

../users/user_d/profile

什么是--- !ruby/hash:ActionController::Parameters action: show controller: profiles user_id: '1' format: '1' ?任何解释都赞赏。

添加我的代码

USER.RB

format: '1'

PROFILE.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

他们的控制器

用户控制器

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

  belongs_to :user
end

PROFILE 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

APP /视图/ PROFILES / _FIELDS.HTML.ERB

<% 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>

ROUTES FOLDER

 <%= 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' %>

3 个答案:

答案 0 :(得分:1)

通常,网址末尾的点后面的点是格式,例如。

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

看起来你在某个地方生成了一个格式错误的url,它将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。

答案 1 :(得分:0)

路线末尾的“点东西”表示您想要获得的格式。 因此,如果你键入profile.json,rails会知道你想要json应答并在控制器中相应地呈现(如果支持这个)。

答案 2 :(得分:0)

其他人已经回答了格式。

我目前正在使用Rails 5.1.5并遇到类似的情况。但是,一旦我删除了我传递的实例变量,id就不会附加到url,你仍然可以在视图中访问它们。

user_profile_path(current_user, @profile)

user_profile_path
相关问题