我是铁杆新手,所以我可能会以某种方式弄乱这个(很明显)。我有一个表单,通过ajax将提交发送回控制器。
这是那个文件 /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>
如果您可以看到其中有一个表单,它会为新项目创建发送ajax请求。
项目控制器在这里
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
最后这是我的create.js.erb文件
$('#yourprojects_<%=current_user.id%>').append('<%=j render partial:"projects/projects", object: @project %>');
/ 项目/ 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文件 /
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
当我在chrome中检查我的网络选项卡时单击提交按钮,它会向/ projects发送一个发布请求,但错误是406不可接受。
有人可以帮我解决这个问题吗。
答案 0 :(得分:0)
尝试将此添加到您的模型中:
attr_accessible :name, :description, :tasks, :assigns, :subtasks, :users
您需要在此attr_accessible语句中包含您计划更新的所有属性。
这是Rails中的安全功能(在Rails 4中更改为强参数)。
答案 1 :(得分:0)
在ProjectsController#create
上,您应该为format.js
条件的else
块提供@project.save
块。例如:
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