gpt4 book ai didi

ruby-on-rails - rails - 错误加载 "new"在 rails 脚手架上使用 ruby​​ 制作的页面

转载 作者:太空宇宙 更新时间:2023-11-03 16:23:03 26 4
gpt4 key购买 nike

我有一个简单的 Rails 应用程序,其中包含设计设置和我创建的配置文件模型。我已经设置了我的关联并且一切似乎都很好,但是当我尝试使用新用户帐户访问 new_profile_path 时,我收到以下错误:

ProfilesController 中的 NoMethodError#newnil:NilClass 的未定义方法 `build'

提取的源代码(大约第 17 行):151617181920

定义新 @profile = current_user.profile.build respond_with(@profile) 结束

问题似乎出在我的配置文件 Controller 中,但我似乎无法弄清楚问题所在。这是我的配置文件 Controller 的完整代码:-

class ProfilesController < ApplicationController
before_action :authenticate_user!
before_action :set_profile, only: [:show, :edit, :update, :destroy]

respond_to :html

def index
@profiles = Profile.all
respond_with(@profiles)
end

def show
respond_with(@profile)
end

def new
@profile = current_user.build_profile
respond_with(@profile)
end

def edit
end

def create
@profile = current_user.build_profile(profile_params)
@profile.save
respond_with(@profile)
end

def update
@profile.update(profile_params)
respond_with(@profile)
end

def destroy
@profile.destroy
respond_with(@profile)
end

private
def set_profile
@profile = Profile.find(params[:id])
end

def profile_params
params.require(:profile).permit(:name, :civil, :email, :level, :employment_date, :mobile, :folder, :title, :internal, :nationality, :vacation, :work_email, :experience)
end
end

我的 html 链接:-

<nav>
<ul>
<li><%= link_to root_path do %>
<i class="fa fa-home"></i>Main
<% end %>
</li>
<% if !current_user.profile.blank? %>
<li><%= link_to profile_path(current_user.profile.id) do %>
<i class="fa fa-user"></i>Employee Profile<i class="fa fa-sort-desc"></i>
<% end %>
<% else %>
<li><%= link_to new_profile_path do %><i class="fa fa-user-plus"></i>Create Profile</li>
<% end %>
<% end %>
<ul>
<li><%= link_to "Tasks / Activities", tasks_path %></li>
<li><%= link_to "Vacations", vacations_path %></li>
</ul>
</li>
<li><%= link_to projects_path do %>
<i class="fa fa-building-o"></i>Projects<i class="fa fa-sort-desc"></i>
<% end %>
<ul>
<li><%= link_to active_path do %>Active Projects<i class="fa fa-caret-right"></i>
<% end %>
<ul>
<li><%= link_to "Preliminary Stage", presignature_path %></li>
<li><%= link_to "Design Stage", develop_path %></li>
<li><%= link_to "Tendered Stage", proposed_path %></li>
</ul>
</li>
<li><%= link_to "Archive", archive_path %></li>
</ul>
</li>
<li><%= link_to project_docs_path do %>
<i class="fa fa-folder-open"></i>Project Documents<i class="fa fa-sort-desc"></i>
<% end %>
<ul>
<li><%= link_to "Preliminary Stage", presignature2_path %></li>
<li><%= link_to development2_path do %>Design Stage<i class="fa fa-caret-right"></i>
<% end %>
<ul>
<li><%= link_to "Phase 1", phase1_path %></li>
<li><%= link_to "Phase 2", phase2_path %></li>
<li><%= link_to "Phase 3", phase3_path %></li>
<li><%= link_to "Phase 4", phase4_path %></li>
</ul>
</li>
<li><%= link_to "Tendered Projects", proposed2_path %></li>
</ul>
</li>
<li><%= link_to search_path do %>
<i class="fa fa-search"></i>Search
<% end %>
</li>
<li><%= link_to reports_path do %>
<i class="fa fa-file-text-o"></i>Reports
<% end %>
</li>
<li><%= link_to feedback_path do %>
<i class="fa fa-comment-o"></i>Feedback
<% end %>
</li>
</ul>
</nav>

我的个人资料模型:

class Profile < ActiveRecord::Base
belongs_to :user
end

我的用户模型:-

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

has_one :profile
end

我希望这个问题足够清楚。提前致谢!

更新:-

将配置文件 Controller 代码更新为上述代码后,我现在收到一个新错误:

配置文件中的 ActionController::UrlGenerationError#new

没有路由匹配 {:action=>"show", :controller=>"profiles", :id=>nil} 缺少必需的键:[:id]

提取的源代码(大约第 8 行):5个6个78个91011

            <% end %>
</li>
<% if !current_user.profile.blank? %>
<li><%= link_to profile_path(current_user.profile) do %>
<i class="fa fa-user"></i>Employee Profile<i class="fa fa-sort-desc"></i>
<% end %>
<% else %>

它说这条线有问题:

  • <%= link_to profile_path(current_user.profile) 做 %>

  • 最佳答案

    current_user 还没有一个profile,所以你不能在它上面调用build。我想你想要的是:

    @profile = current_user.build_profile

    这是 rails 为您提供的自动生成的方法。documentation有点密集,但在这里。 This might be a little more readable .

    关于ruby-on-rails - rails - 错误加载 "new"在 rails 脚手架上使用 ruby​​ 制作的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30628914/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com