-1

In my rails app, i have a project model which can have many project messages.

project has_many project_messages

In projects/show.html.erb, I would like to display all project messages for that project but also create new project messages from this view. Currently, I am not able to create new project messages from this view.

I have reviewed a number of links but none work for me Adding Form For Different Model In Same View, Rails: Show form from different model in a view

My projects/show.html.erb file references as follows:

<div>
 <% render partial: 'project_messages/form', :object => @project_message %>
</div>

In the projects_controller.rb file I have included the following:

  def show
    @project_message = ProjectMessage.new
  end

&

def project_params
  params.require(:project).permit(:title, :description, :phase_id, :RAGStatus, :currentpphase_id,  :project_messages_attributes => [:pMessage, :user_id, :project_id])
end

And in the project.rb file I have the following code also:

accepts_nested_attributes_for :project_messages

project_messages/_form.html.erb

<%= form_with(model: project_message, local: true) do |form| %>
  <% if project_message.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(project_message.errors.count, "error") %> prohibited this project_message from being saved:</h2>

      <ul>
      <% project_message.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

<%= form.label :pMessage %>
<%= form.text_field :pMessage, id: :project_message_pMessage, :class => 'au-input au-input--full au-input--h65', placeholder: 'Type a message' %>
  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

The project_messages form partial does not render so I cannot add a new message from this view.

Error:

undefined local variable or method `project_message' for #<#<Class:0x007f5455ced910>:0x007f5459ef8a30>
Did you mean?  @project_message

When I add @project_message to the partial, it does not render.

EvaB11
  • 35
  • 6

2 Answers2

0

In project_messages/_form.html.erb, this line:

<%= form_with(model: project_message, local: true) do |form| %>

Should be changed to this:

<%= form_with(model: object, local: true) do |form| %>

You assigned a local variable object to a partial, so you should use it.

Also, partials may also be called this way:

<% render partial: 'form', object: @project_message %>
Aaron M.
  • 86
  • 3
0

Normally, when rendering a partial like:

<% render partial: 'form', object: @project_message %>

Because you are passing the @project_message as the specially-named object parameter, this creates a special variable with the name of the partial - in this case, form.

Therefore in the partial, you could reference this variable:

<%= form_with(model: form, local: true) do |form| %>

...However, in this case, it doesn't really make sense to call the local variable form! So I would be inclined to use whatever name you're passing it in as - for example, you could do:

<% render partial: 'form', project_message: @project_message %>

And then in the partial:

<%= form_with(model: project_message, local: true) do |form| %>

For more info, see: http://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables

Tom Lord
  • 27,404
  • 4
  • 50
  • 77
  • Thank you! Thanks working now, using `<%= render partial: 'project_messages/form', locals: { project_message: @project_message } %>` - is there any way of stopping the redirect after it created the message? – EvaB11 Jul 20 '18 at 10:18
  • @EvaB11 That's a separate question, but it sounds like you want to make this an asynchronous form submission - in which case you need to add `remote: true`. There's loads of information about this online, e.g. [this](http://www.korenlc.com/remote-true-in-rails-forms/) – Tom Lord Jul 20 '18 at 10:23
  • great thanks for your help! I have opened a new question for it [here](https://stackoverflow.com/questions/51441779/rails-using-remote-true-to-stop-page-refresh/51443132#51443132) – EvaB11 Jul 20 '18 at 13:15