使用关联为belongs_to的表单将新记录输入数据库



我对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

我的控制器:

在项目中_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

发布中_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中定义创建方法,以打开新的.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"=的项目

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

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

相关内容

  • 没有找到相关文章

最新更新