0

I'm new to rails so I'm probably messing this up somehow (obviously). I have a form that is sending the submission back to the controller via ajax.

This is that file /index.html.erb/

<% provide(:title,'PlanYourTask') %>
<div class="mycontainer">
<div class="myprojcont container">
    <div class="row yourprojects" id="yourproject_<%=current_user.id%>">
        <% if @projects.any? %>
                <%@projects.each do |project| %>
                    <%=render 'projects/projects',:project => project %>
                <%end%>
        <%end%>
            <div class="col-md-3 col-xs-12 newproject">
                    <a class="btn btn-warning" style="border-bottom-left-radius:0px;border-bottom-right-radius:0px;display:block;">Create New Project
                    </a>

                <div class="dropdownnewproject">
                    <div class="projectdropdown" style="background:#bbb;border-bottom-left-radius:5px;border-bottom-right-radius:5px;margin:auto;padding:2px;
                    display:block;">
                    <p class="example-description">Enter the name of the project</p>
                    <%=form_for Project.new,:remote => true do |f|%>
                        <%=f.text_field :name,class:'form-control'%>
                        <%=f.submit class:'col-md-6 btn btn-success'%>
                    <%end%>
                    <a href="#" class="closeproject"><i class="fa fa-times fa-lg" style="margin:10px auto;"></i></a>
                    </div>
                </div>
            </div>
                <script type="text/javascript">
                    $('.newproject > a').click(function(e){
                        $('.dropdownnewproject').slideDown(1000);

                        e.preventDefault();
                    });
                    $('.closeproject').click(function(e){
                        $('.dropdownnewproject').slideUp(1000);
                        e.preventDefault();
                    });
                </script>



    </div>
</div>
</div>

If you can see there is a form inside it which send an ajax request for a new project creation.

The project controller is here

class ProjectsController < ApplicationController
    before_filter :require_user
    def new
        @projects=current_user.projects
        @project=current_user.projects.new
    end

    def create
        @project=current_user.projects.create(params[:project])


        respond_to do |format|
        if @project.save
            format.html{redirect_to :projects}
            format.js

            flash[:success]="Project created successfully"
        else
            render new
        end
    end

    end

    def show
        @project=Project.find(params[:id])

    end

    def index
        @projects=current_user.projects
    end




end

Finally here is my create.js.erb file

$('#yourprojects_<%=current_user.id%>').append('<%=j render partial:"projects/projects", object: @project %>');

/projects/projects.html.erb/

<div class="col-md-3 projects" style="margin-bottom:15px;">
<p style="font-family:TimesNewRoman;font-size:25px;margin:25px auto;text-align:center;"><%=link_to project.name,project,style:'text-decoration:none;'%></p>
</div>

/Project.rb file/

class Project < ActiveRecord::Base
    extend FriendlyId

    friendly_id :name, use: :slugged
    validates :name,presence: :true
    validates :description,presence: :true
    has_many :tasks,dependent: :destroy
    has_many :assigns,dependent: :destroy
    has_many :subtasks,through: :tasks
    has_many :users,through: :assigns


    after_create :add_tasks

    private

    def add_tasks
        self.tasks.build(taskname: "Production")
        self.tasks.build(taskname: "Development")
        self.tasks.build(taskname: "Test")
    end 

end

On clicking the submit button when i check my network tab in chrome,it send a post request to /projects but the error is 406 not acceptable.

Can someone please help me with this.

2 Answers2

1

On ProjectsController#create, you should provide a format.js block for the else block on the @project.save conditional. For example:

if @project.save
  format.html { redirect_to :projects }
  format.js
else
  // add these two formats
  format.html { render :new }
  format.js { json: @project.errors, status: :unprocessable_entity }
end
caike
  • 8,769
  • 2
  • 19
  • 10
  • This is the answer, except that the format.js line is missing `render` in it `format.js { render json: @project.errors, status: :unprocessable_entity }` – grouchomc Oct 17 '22 at 19:51
-1

Try adding this to your model:

attr_accessible :name, :description, :tasks, :assigns, :subtasks, :users

You'll need to include all of the attributes that you plan on updating in this attr_accessible statement.

This is a security feature in Rails (changed to strong parameters in Rails 4).

Andrew Hendrie
  • 6,205
  • 4
  • 40
  • 71