mardi 31 décembre 2019

why select helper does not auto show the selected value?

This code works:

select("questionnaire", "is_active", options_for_select([["no", false], ["yes", true]], @questionnaire.is_active) )

But this will not:

select("questionnaire", "is_active", options_for_select([["no", false], ["yes", true]]) )

Why doesn't rails know to handle the selected value by itself? What's the logic?

lundi 30 décembre 2019

previous_changes does not list the hash changes in ruby

I have a hash column in my table when I change the value of particular hash the changes doesn't list in previous_changes. Can anyone help with this..

But the value gets updated in the table. It happens only for the hash attributes, others works fine. For an example, settings table.

additional_settings column which is of type hash. When I tried changing a key-value pair, e.g.

settings.additional_settings[:key] = "some_value"

The changes does not listed in self.previous_changes

Here is the code in settings.rb,

if self.previous_changes.present? =>(true because of changes in "updated_at" attribute)
  model_changes = self.previous_changes  => (here the change made in hash is not listed)
end

Unable to render different form for same Projectsite

I have a project_site model that saves project attributes. I have created a manager_remark model that has a form for each project_site created. Now I have created a model director and I want to render a different form I have created a director model that has an attribute :remark and : status but i am getting error. is it possible to have a different form for the same project_site.

routes.rb

  resources :project_sites do
    resources :manager_remarks
    resources :director_remarks

  end

project_site_controller.rb

  def index
    @project_sites = current_user.project_sites.order("created_at DESC").paginate(page: params[:page], per_page: 10)
  end

  def show
    @manager_remark = ManagerRemark.new
    @manager_remark.project_site_id = @project_site.id
    @director_remarks = DirectorRemark.new
    @director_remarks.project_site_id = @project_site.id

  end

director_remarks_controller.rb

  def create
    @director_remarks = DirectorRemark.new(remark_params)
    @director_remarks.project_site_id = params[:project_site_id]
    @director_remarks.save

    redirect_to project_site_path(@director_remarks.project_site)
  end

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

director_remark_form.html.erb

<%= form_for [ @project_site, @director_remarks ] do |f| %>
  <div class="row">
    <div class="medium-6 columns">
      <%= f.radio_button :status, true  %>
      <%= f.label :approve %>
      <%= f.radio_button :status, 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 %>

I can render a form for user role manager but I am unable to render a form for user role director for the same project site. Please help.

expose array of objects elements inside the parent object in RAILS

I want to expose subBranches inside array of objects to the parent object as mentioned in Convert To section.

The actual array of objects is :

Original Data :

[
  {
    "ServiceTypeID": 3,
    "ChildCount": 0,
    "IsSelect": 0,
    "subBranches": [
      {
        "ServiceTypeID": 13,
        "ChildCount": 0,
        "IsSelect": 0
      },
      {
        "ServiceTypeID": 14,
        "ChildCount": 0,
        "IsSelect": 0
      },
      {
        "ServiceTypeID": 15,
        "ChildCount": 0,
        "IsSelect": 0
      }
    ]
  },

]

Convert to (basically expose subBranches to parent object):

This is the expected response :

[
  {
    "ServiceTypeID": 3,
    "ChildCount": 0,
    "IsSelect": 0,
    "subBranches":'',
    "ServiceTypeID": 13,
    "ChildCount": 0,       
    "IsSelect": 0,
    "ServiceTypeID": 14,
    "ChildCount": 0,
    "IsSelect": 0,
    "ServiceTypeID": 15,
    "ChildCount": 0,
    "IsSelect": 0
  }
]

How do I get This JSON?

how to add different CSS style class internary operator output in ROR

hi i want to add different CSS style on these two output how can i do that in ruby on rails?

<%= manager_remark.decision ? 'Approved' : 'Rejected' %>

samedi 28 décembre 2019

unable to print the status if neither true nor false in rails

Hi i have i model name ProjectSite and a model name ManagerReamark which takes a decision boolean value. ProjectSite has many ManageRemark. default value of decision is nil. how can i print status as pending when there is no ManagerRemark? here is code

                <% project_site.manager_remarks.each do |manager_remark| %>
              <% if manager_remark.decision == false %>
                <td><%= 'Rejected' %></td>
              <% elsif manager_remark.decision == true %>
                <td><%= "Approved" %></td>
              <% else %>
                <td><%= "Pending" %></td>
              <% end %>
           <% end %>

jeudi 26 décembre 2019

How Do I make Checkboxes retain their value after submitting the form in Rails

This Is the form

<%= form_tag method: "post" do |f| %>
 <script src="https://cdn.iris.nitk.ac.in/design_system/assets/js/vendors/form-components/toggle-switch.js"></script>

<table class="table table-striped">
<thead>
<tr>
  <th></th>
  <th>Title</th>
  <th>Pin To Modules Tab?</th>


</tr>
</thead>

<tbody>
<tr>
  <td class="image"> <%= image_tag("https://cdn.iris.nitk.ac.in/images/new_dashboard/button_Human.png", class: 'list_image') %></td>
  <td>Profile</td>
  <td><%= check_box_tag '1', true, true, :checked => true, data: {toggle: "toggle", on: "Yes", off: "No", onstyle: "success", offstyle: "danger"} %></td>
</tr>
<tr>
  <td class="image"> <%= image_tag("https://cdn.iris.nitk.ac.in/images/new_dashboard/withdraw.png", class: 'list_image') %></td>
  <td>Withdraw Admission</td>
  <td><%= check_box_tag '2', true, true, :checked => true, data: {toggle: "toggle", on: "Yes", off: "No", onstyle: "success", offstyle: "danger"} %></td>
</tr>
<tr>
  <td class="image"> <%= image_tag("https://cdn.iris.nitk.ac.in/images/new_dashboard/attendance.png", class: 'list_image') %></td>
  <td>Feedback Forms</td>
  <td><%= check_box_tag '4', true, true, :checked => true, data: {toggle: "toggle", on: "Yes", off: "No", onstyle: "success", offstyle: "danger"} %></td>
</tr>
<tr>
  <% if can? :my_grade_card, Student %>
  <td class="image"> <%= image_tag("https://cdn.iris.nitk.ac.in/images/new_dashboard/button_manage_users.png", class: 'list_image') %></td>
  <td>My Grade Card</td>
  <td><%= check_box_tag '5', true, true, :checked => true, data: {toggle: "toggle", on: "Yes", off: "No", onstyle: "success", offstyle: "danger"} %></td>
  <% end %>
</tr>
</tbody>
</table>


<%= submit_tag "Pin Modules",:class => "btn btn-primary btn-lg btn-block" %>

This is how i'm saving in my db

def pin_modules

if request.post?
  present_user = User.find(current_user.id)
  present_user.misc_data[:pin_modules]=[]
    (1..17).each do |i|
      (params[i.to_s] ? (present_user.misc_data[:pin_modules] ||= []) << i : next)
    end
  present_user.save!
  redirect_to '/'
end

end

if the value is true then its saved in my db. and for some reason as i have seen the form params values which are only true are passed. i want my form to retain its value either if its true or false for a particular user. can someone help me with this?