使用具有关联belongs_to的表单将新记录输入到数据库中

时间:2015-03-03 13:52:06

标签: ruby-on-rails controller associations belongs-to button-to

我对铁轨上的红宝石比较新,所以请耐心等待。 我正在尝试创建一个应用程序,其中有许多项目具有与每个项目相关的特定问题。以下是我的代码:

我的模特:

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文件,这也是我的根网址:

<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 %>

new.html.erb

<h1>New Issue</h1>

   <%= render 'form' %>

   <%= link_to 'Back', issues_path %>

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

的项目

1 个答案:

答案 0 :(得分: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