beastlyCoder
beastlyCoder

Reputation: 2401

ActionController::InvalidAuthenticityToken When Submitting

I have a program that allows users to enter their song for a certain event. You must enter in the partycode for that event in order for it to submit. Here is a screenshot of what it looks like: enter image description here

When I submit it gives me this error: enter image description here

Here is what my SongsController looks like:

class SongsController < ApplicationController

    def new
        @song = Song.new
    end

    def create
        current_event = Event.find(song_params[:event_id])
        @song = current_event.songs.build(song_params)
        if @song.save
            flash[:success] = "Success"
            redirect_to event_path(@song.event)
        else
            flash[:error] = "Failed"
        end

    end

    def destroy
    end
    private

      def song_params
        params.require(:song).permit(:event_id, :artist, :title, :genre)
      end
end

event model

class Event < ApplicationRecord
  belongs_to :user
  validates :name, presence: true
  validates :partycode, presence: true, length: {minimum: 5}
  has_many :songs, dependent: :destroy  

end

song model

class Song < ApplicationRecord
  belongs_to :event
  validates :artist, presence: true
  validates :title,  presence: true
end

new.html.erb(song)

<br>
<br>
<h1> Member page </h1>
<div class ="container">
  <div class="jumbotron">
    <h2> Select an event to add songs to: </h2>
    <%= form_for Song.new do |f| %>
        <%= f.collection_select(:event_id, Event.all, :id, :name) %>
    <h3> Enter your song </h3>
    <%= form_for Song.new do |f| %>
      <%= f.text_field :artist, placeholder: "Artist" %>
      <%= f.text_field :title,  placeholder: "Title" %>
      <%= f.text_field :genre,  placeholder: "Genre" %>
    <h2> Enter the partycode for that event: </h2>
    <%= form_for Event.new do |f| %>
      <%= f.text_field :partycode %>
      <%= f.submit "Submit", class: "btn btn-primary" %>
    <% end %>
    <% end %>
    <% end %>
  </div>
</div>

What can I do to make this functionality of my website work? Any help is greatly appreciated thanks

Upvotes: 1

Views: 195

Answers (2)

dp7
dp7

Reputation: 6749

You have completely messed up your form. Ideally you should have one form but here you have just kept one form within another form using form_for.

I would recommend you to have a look at the form_for documentation .

Upvotes: 1

Duyet Nguyen
Duyet Nguyen

Reputation: 543

I see many form_for nesting on your views. It's impossible. Only one submit for a form.

I think you may want to change your _form.html.erb

<div class ="container">
  <div class="jumbotron">
    <h2> Select an event to add songs to: </h2>
    <%= form_for @song do |f| %>
      <%= f.collection_select(:event_id, Event.all, :id, :name) %>
      <h3> Enter your song </h3>
      <%= f.text_field :artist, placeholder: "Artist" %>
      <%= f.text_field :title,  placeholder: "Title" %>
      <%= f.text_field :genre,  placeholder: "Genre" %>
      <h2> Enter the partycode for that event: </h2>
      <%= f.text_field :partycode %>
      <%= f.submit "Submit", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

Upvotes: 1

Related Questions