基本上,这是我的控制器:
class Api::ProjectsController < ApplicationController
before_action :set_params, only: [:show]
respond_to :json
def new
@project = Project.new
render layout: false
end
def create
@project = Project.new allowed_params
@project.sample = false
@project.users << current_user
if @project.save
@project.members << current_user
# redirect_to project_path(@project)
else
end
render json: {new_project: @project}
end
def set_params
@page = params[:page]
@client = params[:client]
@block = params[:block]
@block = Block.find @block if @block
end
def allowed_params
params.require(:project).permit :name, :sample
end
end
这是我的html代码段:
<form ng-submit='createStream()'>
<input autofocus='autofocus' class='form-control' id='project_name' placeholder='Untitled Stream' required='required' type='text', ng-model='newProject.name'>
<br/>
<input type='submit' value='Create' class='btn btn-md btn-success'>
</form>
我的popovercontroller.js.coffee
App.controller("PopoverCtrl", ($scope, Project, $location, $http) ->
$scope.initializeProject = ->
$scope.newProject = new Project();
$scope.createStream = ->
project = Project.save($scope.newProject)
$scope.projects.push(project)
)
服务/工厂
App.factory 'Project', ($resource) ->
$resource "/api/projects/:id", {id: @id},
query:
method: "POST"
isArray: false
我可以将数据保存到我的数据库中,但是我希望在保存数据后重定向到show视图。
就像RAILS的默认脚手架一样,在提交表单后,如果它有效......它将重定向到其显示页面。 (例如localhost:3000/projects/1
及其ID。
但是,现在的行为是在提交表单后,它不会重定向到显示页面。我正在尝试获取已保存记录的ID但没有运气。
我尝试了$location.path('/projects/' + @id)
,但似乎@id
未定义且无法识别。我是使用Angular操纵JSON的新手。
编辑:这是我的控制台日志。我认为JSON没有回来?它重定向到我来自的页面。我的表单采用popover样式,因此没有进行重定向。
Started POST "/api/projects" for 127.0.0.1 at 2014-01-30 06:38:30 +0800
Processing by Api::ProjectsController#create as HTML
Parameters: {"name"=>"XXX", "project"=>{"name"=>"XXX"}}
User Load (2.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 14 ORDER BY `users`.`id` ASC LIMIT 1
(0.2ms) BEGIN
SQL (0.3ms) INSERT INTO `projects` (`created_at`, `name`, `updated_at`) VALUES ('2014-01-29 22:38:30', 'XXX', '2014-01-29 22:38:30')
(0.2ms) INSERT INTO `projects_users` (`project_id`, `user_id`) VALUES (91, 14)
(16.4ms) COMMIT
(0.2ms) BEGIN
SQL (0.3ms) INSERT INTO `project_memberships` (`member_id`, `project_id`) VALUES (14, 91)
(1.2ms) COMMIT
Completed 200 OK in 53ms (Views: 0.5ms | ActiveRecord: 22.3ms)
Started GET "/projects/23" for 127.0.0.1 at 2014-01-30 06:38:30 +0800
Started GET "/?goto=projects/23" for 127.0.0.1 at 2014-01-30 06:38:30 +0800
Processing by MainController#index as HTML
Parameters: {"goto"=>"projects/23"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 14 ORDER BY `users`.`id` ASC LIMIT 1
Rendered main/index.html.haml within layouts/main (0.1ms)
Rendered application/_header.html.slim (0.5ms)
Rendered application/_footer.html.slim (0.2ms)
Completed 200 OK in 40ms (Views: 36.0ms | ActiveRecord: 0.4ms)