Ruby on rails 与URL生成错误不匹配的路由

Ruby on rails 与URL生成错误不匹配的路由,ruby-on-rails,ruby,Ruby On Rails,Ruby,我对Rails非常陌生。请原谅我的问题。我正在创建一个应用程序,用户可以上传故事供其他人阅读。登录时,他们也可以创建自己的个人资料。现在,一旦用户提交故事,在故事索引页面,我尝试列出故事标题以及提交者的姓名。我尝试将提交者的姓名作为链接,显示提交者的个人资料显示页面。但是这似乎不起作用。我尝试了各种方法,但没有给出解决方案。 这是我的用户模型 class User < ActiveRecord::Base devise :database_authenticatable, :registe

我对Rails非常陌生。请原谅我的问题。我正在创建一个应用程序,用户可以上传故事供其他人阅读。登录时,他们也可以创建自己的个人资料。现在,一旦用户提交故事,在故事索引页面,我尝试列出故事标题以及提交者的姓名。我尝试将提交者的姓名作为链接,显示提交者的个人资料显示页面。但是这似乎不起作用。我尝试了各种方法,但没有给出解决方案。 这是我的用户模型

class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, 
:recoverable, :rememberable, :trackable, :validatable,
:authentication_keys => [:login]
has_many :stories
has_one :profile, dependent: :destroy
attr_accessor :login
validate :validate_username
这是我的个人资料控制器

before_action :authenticate_user!
#before_action :find_profile, only: [:show, :edit, :update, :destroy]

def index
  @profiles = Profile.all
end

def new
    @profile = Profile.new
end

def create
  @profile = Profile.new(profile_params)
  @profile.user_id = current_user.id if current_user
    if @profile.save
  flash[:success] = "Profile saved"
  redirect_to root_path
  else
  flash[:error] = "Error"
  render :new
  end
end

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

def update
    @profile = Profile.find(params[:id])
  if @profile.update_attributes(profile_params)
  flash[:success] = "Successfully updated"    # Optional
  redirect_to profile_path
  else
  flash[:error] = "Error"       # Optional
  render :edit
end
end

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

def destroy
    @profile = Profile.find(params[:id])
    @profile.destroy

    redirect_to root_path
end

private

 def profile_params
    params.require(:profile).permit
   ( :first_name, :last_name, :date_of_birth, :occupation, 
   :hobbies,   :about_me)
 end
这是我的故事索引页面,我尝试在其中列出提交故事的用户名

 <ul>
 <p>
 <% @stories.each do |story| %>
 <li><h1><%= link_to story.title, story %></h1>
 (<%= link_to story.category.category_name, 
 categories_path(@categories)  %>)
 Submitted by <%= link_to story.user.username ,
 profile_path(@profile) %>   &nbsp on 
 <%= story.created_at.strftime("%B-%d-%Y") %> </li>
 <% end %>
 </p>
 <br/>

 <div class="digg_pagination">
 <div class="page_info">
 <%= page_entries_info @stories %>
 </div>
 <%= will_paginate @stories, :container => false %>
 </div>
  • () 提交人:年月日

  • 假%>
在这里点击“story.user.username”应该会引导我进入用户的个人资料显示页面

ActionController::故事中的UrlGenerationError#索引 显示/home/mahesh/storyteller/app/views/stories/index.html.erb,其中第5行出现:

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


请帮忙。另外,如果我必须在此处发布更多信息,请告诉我,@profile以nil传递,请检查profile对象,或者您正在尝试检查story用户配置文件,然后在内部传递story.user

配置文件路径(story.user)


.

由于您是动态web开发的新手,您的想法对大多数有经验的人来说都非常简单。我决定按照rails的枯燥实践,以最少的风格构建应用程序的基本功能。是的

程序开发人员很难用您提供的信息,再加上我在上面看到的语法,为您的应用程序提供一个明确的答案,您的应用程序也可能存在一些隐藏问题

在应用程序函数及其属性的描述中使用编程术语也是一种很好的做法^R是一种复杂的OOP语言,其中R$是一种MVC架构框架,如果您不能使用PT正确地描述它,那么开发人员很难帮助您。成功成为一名优秀而热心的专业开发人员需要经历多次失败。(坚持)是关键价值=“勤奋”


在我为您开发的供您学习的网站中,我为RoR提供了一些很好的在线资源。请按照自述说明进行操作。快乐编码

显示你的故事索引方法。。。
link\u to
方法中的
@profile
可能是
nil
尝试将
profile\u路径(@profile)
中的
@profile
替换为
story.user
类似于
profile\u路径(story.user)
是的..成功了..谢谢你,米纳图克..非常感谢..我已经克隆了你的应用程序并将进行检查。我想我还有很多东西要学。我希望我能向你寻求更多指导…@Michael DevenportHi Mahesh,是的,如果你需要帮助,欢迎你联系。去给我发个口信。也在快乐编码找到我!
 <ul>
 <p>
 <% @stories.each do |story| %>
 <li><h1><%= link_to story.title, story %></h1>
 (<%= link_to story.category.category_name, 
 categories_path(@categories)  %>)
 Submitted by <%= link_to story.user.username ,
 profile_path(@profile) %>   &nbsp on 
 <%= story.created_at.strftime("%B-%d-%Y") %> </li>
 <% end %>
 </p>
 <br/>

 <div class="digg_pagination">
 <div class="page_info">
 <%= page_entries_info @stories %>
 </div>
 <%= will_paginate @stories, :container => false %>
 </div>