我创建了一个应用程序,其中用户HAS_ONE轮廓和belongs_to的用户配置文件。 它的设置使用户创建时自动创建配置文件(在用户控制器中)为什么我不能显示编辑的形式?
我不想设置它,因此用户可以访问编辑页面(配置文件控制器 - >编辑操作)和通过渲染的形式修改个人资料信息(使用_form部分)
我不明白为什么我不能让编辑表单中显示,据我可以告诉方法是否正确定义?任何人有任何想法为什么?非常感谢任何帮助!
浏览器:
NoMethodError in ProfilesController#edit
undefined method `user' for nil:NilClass
def edit
@profile = @profile.user
end
def destroy
资料控制器:
class ProfilesController < ApplicationController
before_action :logged_in_user, only: [:edit, :destroy]
def edit
@profile = @profile.user
end
def destroy
end
end
用户控制器
class UsersController < ApplicationController
before_action :logged_in_user, only: [:edit, :update]
before_action :correct_user, only: [:edit, :update]
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
@user.profile = Profile.new
@user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to root_url
else
render 'new'
end
end
...
组的意见/型材/ edit.html.erb
<% provide(:title, 'Edit Profile') %>
<h1>Editing profile</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@profile) do |f| %>
<%= render 'form', f: f %>
<%= f.submit "Edit Information", class: "btn btn-primary" %>
<% end %>
</div>
</div>
的意见/型材/ _form.html.erb
<%= render 'shared/error_messages' %>
<%= f.label :first_name %>
<%= f.text_field :first_name, class: 'form-control' %>
...
你使用设计来处理用户身份验证?如果你是(和恕我直言,你应该)你应该可能使用@profile = current_user.profile,与您目前的设置,我可能能够编辑任何用户配置文件,只需更改URL中的ID – Richlewis 2014-09-25 08:09:40
谢谢!做到了!这是一个明显的答案!我一整天都在编码,所以我认为我的大脑刚开始融化。我建立了我自己的身份验证,所以我可以更好地了解工程─我不喜欢一切到底是怎样设计,因为我看不到自己的控制器和不喜欢不理解发生了什么事继承。 – sss333 2014-09-25 08:19:05
我可以接受你的答案,如果你发布它作为一个?你是说我应该用色器件,因为我还没有建成就足够阻止对某人潜在的编辑通过改变URL中的个人资料? – sss333 2014-09-25 08:27:35