【问题标题】:how can i render form from other controller using link_to in rails?如何使用 rails 中的 link_to 从其他控制器呈现表单?
【发布时间】:2020-01-13 11:27:33
【问题描述】:

您好,有项目脚手架,并且有项目列表。我已经创建了一些领域的舞台脚手架。现在我想在项目视图的显示页面中呈现舞台形式。我试过了,但我得到了错误。我想在项目的展示页面中使用一个link_to以舞台的形式展示。

routes.rb

Rails.application.routes.draw do
  resources :stages
  resources :projects
end

项目 show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <strong>Project name:</strong>
  <%= @project.project_name %>
</p>
<%= link_to 'Edit', edit_project_path(@project) %> |
<%= link_to 'Back', project_managers_path %>

<br>
<br>
<%= link_to "Add Stage", stage_form_path %>

project_controller.rb

  def index
    @projects = current_user.projects.all.paginate(page: params[:page], per_page: 15)
  end


  def show
    @stages = Stage.new
  end

每个项目在模型中有很多阶段关系。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-5


    【解决方案1】:

    你的答案是:

      <%= link_to "Add Stage", form_stage_path %>
    

    如果对路由感到困惑,请在实施它们之前在项目的控制台中运行此命令(linux):

    rails routes (rake routes in older rails versions)
    
    rails routes | grep stage
    
    // the above command is specific to your case but grep searches for answers containing the string given to it (stage in this case).
    

    【讨论】:

    • 这个link_to给出NameError
    • @Anuj 更新,有一个错字。先运行这个rails routes | grep stage,看看你需要给出的路径。
    • @Anuj 是 form_stage_path 还是别的什么?
    • 它是 form_stage_path
    • 请将您当前的错误粘贴到问题的末尾。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 2013-10-22
    • 1970-01-01
    相关资源
    最近更新 更多