0

I am using following form and controller. If I create a new notification everything gets saved except the campus_id.

It seems to give the wrong campus parameter although I select a different one from the dropdown. If I edit the same entry afterwards then it does get saved? What is going on and how do I fix it?

The same form is used for the edit and create actions. (it is a partial)

It might be worth noting that I use shallow routes for the campus (has_many) and notifications(belongs_to).

routes.rb

  shallow do
    resources :campus do
      resources :notifications
    end
  end

Form:

<%= form_for [@campus,@notification] do |f| %>
  <% if @notification.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@notification.errors.count, "error") %> prohibited this notification from being saved:</h2>

      <ul>
      <% @notification.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :post %><br>
    <%= f.text_area :post %>
  </div>
  <div class="field">
    <%= f.label :campus %><br>
    <%= f.collection_select(:campus_id, Campus.all.order('name ASC'), :id, :name, prompt: true) %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

This is the controller:

class NotificationsController < ApplicationController
  before_action :set_notification, only: [:show, :edit, :update, :destroy]
  before_action :set_campus, only: [:index, :new, :create]

  def index
    @notifications = @campus.notification
  end

  def show
  end

  def new
    @notification = @campus.notification.new
  end

  def edit
  end

  def create
    @notification = @campus.notification.new(notification_params)

    respond_to do |format|
      if @notification.save
        format.html { redirect_to @notification, notice: 'Notification was successfully created.' }
        format.json { render action: 'show', status: :created, location: @notification }
      else
        format.html { render action: 'new' }
        format.json { render json: @notification.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @notification.update(notification_params)
        format.html { redirect_to @notification, notice: 'Notification was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @notification.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @notification.destroy
    respond_to do |format|
      format.html { redirect_to campu_notifications_url(1) }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_notification
      @notification = Notification.find(params[:id])
    end

    def set_campus
      @campus = Campus.find(params[:campu_id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def notification_params
      params.require(:notification).permit(:post, :campus_id)
    end
end

If I look at the log I see the wrong parameter is comitted.

Started POST "/campus/1/notifications" for 84.193.153.106 at 2014-09-29 18:29:33 +0000 Started POST "/campus/1/notifications" for 84.193.153.106 at 2014-09-29 18:29:33 +0000 Processing by NotificationsController#create as HTML Processing by NotificationsController#create as HTML Parameters: {"utf8"=>"_", "authenticity_token"=>"oNSlEFeukwEj2hIAT89wFdIYwjHO5c8lzBlCqMyk31Y=", "notification"=>{"post"=>"sdqfdsfd", "campus_id"=>"3"}, "commit"=>"Create Notification", "campu_id"=>"1"} Parameters: {"utf8"=>"_", "authenticity_token"=>"oNSlEFeukwEj2hIAT89wFdIYwjHO5c8lzBlCqMyk31Y=", "notification"=>{"post"=>"sdqfdsfd", "campus_id"=>"3"}, "commit"=>"Create Notification", "campu_id"=>"1"} Campus Load (0.4ms) SELECT "campus".* FROM "campus" WHERE "campus"."id" = $1 LIMIT 1 [["id", "1"]] Campus Load (0.4ms) SELECT "campus".* FROM "campus" WHERE "campus"."id" = $1 LIMIT 1 [["id", "1"]] (0.1ms) BEGIN (0.1ms) BEGIN SQL (28.6ms) INSERT INTO "notifications" ("campus_id", "created_at", "post", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["campus_id", 1], ["created_at", Mon, 29 Sep 2014 18:29:34 UTC +00:00], ["post", "sdqfdsfd"], ["updated_at", Mon, 29 Sep 2014 18:29:34 UTC +00:00]] SQL (28.6ms) INSERT INTO "notifications" ("campus_id", "created_at", "post", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["campus_id", 1], ["created_at", Mon, 29 Sep 2014 18:29:34 UTC +00:00], ["post", "sdqfdsfd"], ["updated_at", Mon, 29 Sep 2014 18:29:34 UTC +00:00]] (3.5ms) COMMIT (3.5ms) COMMIT

Christoph
  • 1,347
  • 2
  • 19
  • 36
  • Everything looks right. Take a look into your log/development.log while submit the form. It can help! – Rodrigo Sep 29 '14 at 18:20

1 Answers1

2

Might want to change your new and create actions like this:

def new
  @notification = @campus.notifications.build
end

def create
  @notification = @campus.notifications.build(notification_params)

  respond_to do |format|
    if @notification.save
      format.html { redirect_to @notification, notice: 'Notification was successfully created.' }
      format.json { render action: 'show', status: :created, location: @notification }
    else
      format.html { render action: 'new' }
      format.json { render json: @notification.errors, status: :unprocessable_entity }
    end
  end
end

campus.build_notification will instantiate a notification that belongs_to campus. Using new would require you to pass notification[campus_id] as part of your params.

sjaime
  • 1,480
  • 9
  • 16
  • I am getting a undefined method `build_notification' for # error when trying that. – Christoph Sep 29 '14 at 18:47
  • I tried @campus.notification.build and I got the same problem. – Christoph Sep 29 '14 at 18:50
  • Misread the direction of your associations. Should be `@campus.notifications.build` – sjaime Sep 29 '14 at 18:51
  • If I use @campus.notifications.build (plural) I get a no method error. If I use it singular the errors stays the same as before. I am using nested shallow routes, does that get something to do with it? – Christoph Sep 29 '14 at 19:00
  • Well, it shouldn't, as long as campus `has_many` notifications. What error do you get if you use `@campus.notifications.build` plural? Also, since `@campus` is loaded from the URL param, you might want to drop the `f.collection_select :campus_id` in the form, as long as you want to associate the new notification with @campus. – sjaime Sep 29 '14 at 19:22
  • I had has_many :notification (singular) in my model. I edited and made it plural. I still have the same problem with the saving. I wanted to make a form which was able to assign to whatever campus the user likes. Either way perhaps it's better to drop it and do it that way. If somebody still finds a solution I'll be happy to hear from you. And thanks for your help sjaime! – Christoph Sep 29 '14 at 19:37