I am trying (and failing miserably) to understand some basic things about rails. I am a student on code school, gorails and a bunch of others. I'm not figuring out how to join the dots on these basic things - and it's driving me nuts - Im 4+ years in to trying to learn but I have not managed to grasp the basic building blocks of rails.
I have models called Organisation and Stance::Assessment. The associations are:
Organisation
has_one :assessment, class_name: Stance::Assessment, inverse_of: :organisation
accepts_nested_attributes_for :assessment, reject_if: :all_blank, allow_destroy: true
Stance::Assessment
belongs_to :organisation, inverse_of: :assessment
My routes are:
resources :organisations
namespace :stance do
resources :assessments
end
On my organisation show page, I'm trying to add a link allowing a user to assess the organisation's policy. The Stance::Assessment resource has the form to provide this assessment.
On my organisation show page, I have:
<%= link_to 'Assess this policy', new_stance_assessment_path(organisation: @organisation), class:'btn btn-blue btn-line' %>
In the bracketed part of this path, I'm trying to get the stance_assessment path to recognise the organisation that is currently showing.
In the stance assessment form, I have:
<%= simple_form_for(@assessment) do |f| %>
<%= f.error_notification %>
<% @assessment.errors.full_messages.each do |msg| %>
<%= li= msg %>
<% end %>
<div class="form-inputs" style="margin-bottom: 50px">
<%= f.hidden_field :organisation_id, :value => @organisation.id %>
<div class="row">
<div class="col-md-10 col-md-offset-1" style="margin-top: 50px">
<%= f.input :comment, as: :text, label: "Assess this policy", :input_html => {:rows => 10} %>
</div>
</div>
</div>
I have tried to give a hidden field so that the organisation_id saved in the stance_assessment table is the organisation.id of the path that led to this form. That part is a guess -I've tried lots of variations on where to put this and how to express it, but I can't find anything that works.
In my stance assessment controller, new action, I'm trying to define the organisation. I have:
def new
@assessment = Stance::Assessment.new
@organisation = Organisation.find(params[:id])
# authorize @assessment
end
However, this isn't any good, because it generates an error that says:
Couldn't find Organisation with 'id'=
I don't understand what this error means. All of my organisations have an :id attribute.
If it's something to do with the organisation_id attribute in the stance assessments table not being set by the time it's trying to do the new action (which does make sense), I'm then lost for how to set this when the form will always need to use the new action.
In my organisation controller, I have built the association with assessment as:
def new
@organisation = Organisation.new
@organisation.build_assessment
This all works fine if I nest the form fields for assessment inside of the organisation form. I don't want to do that because I want a different user (not the organisation owner) to complete the assessment - so I want the assessment form inputs in a different form.
Can anyone help?
Aucun commentaire:
Enregistrer un commentaire