简体   繁体   中英

how can i get project_id by remarks in ruby on rails

I have manager remark model that takes input as a remark and decision value and saves it with the project site ID. I have a project site model that takes input as name, date, and file and stores it. Many remarks have a many to one relation with project site ID, and the project site belongs to the manager remark. I want to access the decision attribute boolean value in project site index form, but I am unable to access that boolean value in the index page of the project site. Here is my code of project site and manager remarks model, view and controller-

project site index.html.erb

    <table>
    <thead>
      <tr>
        <th>Name</th>
        <th>Date</th>
        <th>Attendance</th>
        <th>Status</th>
        <th colspan="3"></th>
      </tr>
    </thead>

    <tbody>
      <% @project_sites.each do |project_site| %>
        <tr>
          <td><%= project_site.name.titleize %></td>
          <td><%= project_site.date %></td>
          <td><%= link_to ' View attendance', project_site.file, :class => "fi-page-export-csv" %></td>
          <td><%= "here i want to access manager remark decision value" %></td>
          <td><%= link_to 'Remark ', project_site %><span>(<%= project_site.manager_remarks.size %>)</span></td>
          <td><%= link_to 'Edit', edit_project_site_path(project_site) %></td>
          <td><%= link_to 'Destroy', project_site, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        </tr>
      <% end %>
    </tbody>

project site controller

    def index
    @project_sites = ProjectSite.all.order("created_at DESC")
    @manager_remark = ManagerRemark.joins(:project_site).where(:project_sites => { :user_id => @user.id })
  end

  # GET /project_sites/1
  # GET /project_sites/1.json
  def show
    @manager_remark = ManagerRemark.new
    @manager_remark.project_site_id = @project_site.id

  end

  # GET /project_sites/new
  def new
    @project_site = ProjectSite.new
  end
  def project_site_params
      params.require(:project_site).permit(:name, :date, :file)
    end

manager_remark controller

    class ManagerRemarksController < ApplicationController

  def create
    @manager_remark = ManagerRemark.new(remark_params)
    @manager_remark.project_site_id = params[:project_site_id]
    @manager_remark.save

    redirect_to project_site_path(@manager_remark.project_site)
  end

  def remark_params
    params.require(:manager_remark).permit(:remark, :decision)
  end

end

manager_remark view form


<%= form_for [ @project_site, @manager_remark ] do |f| %>
  <div class="row">
    <div class="medium-6 columns">
      <%= f.radio_button :decision, true  %>
      <%= f.label :approve %>
      <%= f.radio_button :decision, false  %>
      <%= f.label :reject %>
    </div>
    <br>
    <br>
    <div class="medium-6 cloumns">
      <%= f.label :remark %><br/>
      <%= f.text_area :remark %>
    </div>

      </div>
    <div>
      <%= f.submit 'Submit', :class => 'button primary' %>
    </div>

<% end %>

routes.rb

    Rails.application.routes.draw do
  root to: 'home#index'
  devise_for :users
  resources :project_sites do
    resources :manager_remarks

  end

  get '/project_manager_level_two' => 'project_manager_level_two#index'
  get '/project_managers' => 'project_managers#index'

  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

If I understand correctly, you have a ProjectSite that contains a ManagerRemark with a decision, right? If that's the case, the simple answer is:

<%= project_site.ManagerRemark.decision %>

If you are saying that each ProjectSite has many ManagerRemark s, you'll want to place the above inside a loop, like so:

<% project_site.manager_remarks.each do |manager_remark| %>
<%= manager_remark.decision %><br/>
<% end %>

This assumes that your models are correctly configured to recognize these relationships. The above may also be optimized by adding an include clause to your fetch inside the controller and there's no need to fetch the ManagerRemark objects separately. Therefore, you'd probably want something like:

def index
    @project_sites = ProjectSite.all.includes( :manager_remark ).order("created_at DESC")
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM