vendredi 30 juin 2017

Ruby on Rails undefined method 'due_date' #

I'm just new in RoR and I encountered this problem. I am making a system, basically a system that manages dorm transactions. so i have a users_controller, accountabilities_controller (for dormer's accountabilities), and violations controller. i have this snippet in my index.html.erb for my users:

                <% @users.each do |user| %>
                    <% if !(user.admin?) && !user.archived? %>
                        <tr>
                            <td><%= user.id %></td>
                            <td><%= link_to "#{user.first_name}, #{user.last_name} #{user.mi}", url_for(user) + '/personal-info' %></td>
                            <td><%= user.student_no %></td>
                            <td><%= user.email %></td>
                            <td><%= user.sts_bracket %></td>
                            <td>
                                <% if user.activated? %>
                                    Activated
                                <% else %>
                                    Not Activated
                                <% end %>
                            </td>
                            <td>
                                <%= link_to "Edit", (user.activated ? edit_ddms_user_path(user) : '#'), class: 'btn btn-primary' %>
                                <%= link_to "Archive", change_ddms_user_path(user), data: {confirm: "Are you sure?"}, class: 'btn btn-primary'%> 
                                <%= link_to "Add accountability", ddms_accountabilities_new_path(:user_id => user.id), class: 'btn btn-primary' %>
                                <%= link_to "Add violation", ddms_violations_new_path(:user_id => user.id), class: 'btn btn-primary' %>
                            </td>
                        </tr>
                    <% end %>
                <% end %>

as you can see, i have a link/button for adding violations and accountabilities. when i add accountability, i don't encounter a problem. but when i try to add violation, everytime i submit the form, i get this nomethoderror:

nemethoderror: undefined method 'due_date' for ddms_violation..

I've searched other problems like this before and usually get it to work. the problem is usually found in the defined params. but one thing that is bothering me in this situation is that the 'due_date' attribute is not in my violation_params:

 def violation_params
      params.require(:ddms_violation).permit(:title, :remarks, :user_id)
 end

because 'due_date' is not an attribute even in my violations table:

class CreateDdmsViolations < ActiveRecord::Migration[5.0]
  def change
    create_table :ddms_violations do |t|
      t.string :title
      t.text :remarks
      t.boolean :resolved, default: false
      t.integer :user_id

      t.timestamps
    end
  end
end

and this 'due_date' is an attribute in my accountability table:

def change
    create_table :ddms_accountabilities do |t|
      t.string :accountability
      t.text :remarks
      t.boolean :resolved, default: false
      t.datetime :due_date

      t.timestamps
    end
end

I am not having any problems with my accountabilities index. so why is it that due_date is still recognized as a part of params in violations? I have read that everytime I render to 'new' I am given a new set of params. I also read that it is a big NO to manually manipulate values in params.

p.s In the calling of 'new' in violations an accountabilities controllers, i am passing a user_id parameters since i am not using table associations. this way i am passing a user_id in a hidden_field in the form. anyway, I am not sure if this is a probable cause of the problem I mentioned above.

Responses will be pretty much appreciated! Thank you!!

Aucun commentaire:

Enregistrer un commentaire