1

我对 ruby​​ on rails 比较陌生,所以请耐心等待。我正在尝试创建一个应用程序,其中有许多与每个项目相关的特定问题的项目。以下是我的代码:

我的模型:

class Issue < ActiveRecord::Base
belongs_to :project
end

class Project < ActiveRecord::Base
has_many :issues
has_and_belongs_to_many :users
validates :name, :description, :manager, presence: true
validates :name, uniqueness: true
end

我的控制器:

在 project_controller.rb

def create
@project = Project.new(project_params)

respond_to do |format|
  if @project.save
    format.html { redirect_to @project, notice: "Project #{@project.name} was successfully created." }
    format.json { render :show, status: :created, location: @project }
  else
    format.html { render :new }
    format.json { render json: @project.errors, status: :unprocessable_entity }
  end
end
end

在issues_controller.rb

def create        
@issue = Issue.new(params[:issue])
project = Project.find(params[:project_id])


respond_to do |format|
  if @issue.save
    format.html { redirect_to root_path, notice: "A new Issue was successfully added to project #{project.name}." }
    format.json { render :show, status: :created, location: @issue }
  else
    format.html { render :new }
    format.json { render json: @issue.errors, status: :unprocessable_entity }
  end
end
end

我的项目索引 html 文件也是我的根 url:

<tbody>
<% @projects.each do |project| %>
  <tr>
    <td><%= project.name %></td>
    <td><%= project.manager %></td>
    <td><%= project.description %></td>
    <td><%= project.created_at.strftime("%d %B %Y @ %H:%M") %></td>
    <td><%= button_to 'Report An Issue', new_issue_path(project_id: project) %>
    <td><%= link_to 'Show', project %></td>
    <td><%= link_to 'Edit', edit_project_path(project) %></td>
    <td><%= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>

我的问题是我应该如何在 issues_controller.rb 中定义 create 方法以打开 new.html.erb 表单从用户那里获取输入,然后保存在数据库中,同时链接到报告问题按钮引用的项目。我可以查看的任何答案、见解或其他链接将不胜感激。

_form.html.erb

    <%= form_for(@issue) do |f| %>
  <% if @issue.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@issue.errors.count, "error") %> 
      prohibited this  issue from being saved:</h2>

      <ul>
      <% @issue.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <fieldset>
  <legend><strong>Enter Issue Details</strong></legend>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </div>
  <div class="field">
    <%= f.label :type %><br>
    <%= f.text_field :type %>
  </div>
  <div class="field">
    <%= f.label :status %><br>
    <%= f.text_field :status %>
  </div>
  <div class="field">
    <%= f.label :reporter %><br>
    <%= f.text_field :reporter %>
  </div>
  <div class="field">
    <%= f.label :remarks %><br>
    <%= f.text_area :remarks %>
  </div>
  <div class="field">
    <%= f.label :priority %><br>
    <%= f.text_field :priority %>
  </div>
  <div class="field">
    <%= f.label :assignedto %><br>
    <%= f.text_field :assignedto %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
  </fieldset>
  <% end %>

新的.html.erb

<h1>New Issue</h1>

   <%= render 'form' %>

   <%= link_to 'Back', issues_path %>

我必须手动重新输入 url /issues/new?project_id=15 才能打开表单页面,否则我会收到路由错误,提交后它给了我找不到带有 'id'= 的项目

4

1 回答 1

0

您在正确的轨道上,您已经创建了一个关联,现在您只需要在 create 方法中引用该项目,如下所示:

def new 
  @project = Project.find params[:project_id]
  @issue = @project.issues.build 
end


def create  
  @project = Project.find(params[:project_id])      
  @issue = @project.issues.build(issue_params)
  ## rest of code
end

然后在你的 _form.html.erb 你现在应该有:

form_for [@project,@issue] 
   #rest of code
于 2015-03-03T14:48:41.977 回答