【问题标题】:redirect_to action in Create issue Ruby on Rails在创建问题 Ruby on Rails 中的 redirect_to 操作
【发布时间】:2016-10-12 05:53:54
【问题描述】:

我正在构建一个带有用户身份验证的小 Rails 应用程序。这是一个应用程序,您可以在其中组织项目、在其中添加任务、添加一些 cmets。

当我是当前用户时,我可以单击“启动项目”并查看表单。但是当我提交表单时,它会将我重定向到项目索引(这是我想要的),但它不会保存项目;我有错误消息。无论如何,你可以用代码看到它:

这是我的project.rb

class Project < ActiveRecord::Base

belongs_to :user

validates_presence_of :name, :deadline, :description

attr_accessor :name, :deadline, :description

end

这是我的projects_controller.rb

class ProjectsController < ApplicationController
  include ActiveModel::Model
  attr_accessor :name, :deadline, :description

  validates :name, presence: true
  validates :deadline, presence: true
  validates :description, presence: true

  def index
    @projects = Project.all
  end

  def new
    @project = Project.new
  end

  def create
    @project = Project.new(project_params)
    if @project.save
      redirect_to projects_show_path, notice: 'Project successfully added'
    else
      flash[:alert] = 'An error ocurred adding your project. Please try again later'
      redirect_to projects_index_path
    end
  end

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

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

  def update
    @project = Project.find(params[:id])
    if @project.update_attributes(project_params)
      redirect_to projects_index_path
    else
      flash[:alert] = "An occurred editing your project. Please try again later"
      redirect_to projects_index_path
    end
  end

  def destroy
    @project = Project.find(params[:id])
    if @project.destroy
      redirect_to projects_index_path
    else
      flash[:alert] = "An error occured deleting your project... Please try again later"
      redirect_to projects_index_path
    end
  end


  private

  def project_params
    params.require(:project).permit(:name, :deadline)
  end



end

这是我的projects/index.html.erb

<body id="projects-index-body">
  <div id="welcome-projects">
    <h1>Hi <% @current_user %> ! Start managing your projects !</h1>
  </div>

  <div id="projects-index-container>">
    <% if @project.nil? %>
      <div id="no-project">
        <p>You don't have any projects <%= link_to 'yet', projects_new_path %>.</p>
      </div>
      <% else %>
        <%= render @projects %>
      <% end %>
  </div>
</body>

这是我的_form.html.erb

<div id="form-new-project-container">
  <%= form_for @project do |f| %>
    <div id="form-input">
      <%= f.text_field :name, placeholder: 'Project name', autofocus: true %>
    </div>
    <div id="form-input">
      <%= f.text_field :deadline, placeholder: 'What is your project deadline ?', autofocus: true %>
    </div>
    <div id="form-input">
      <%= f.text_area :description, placeholder: 'Add a description', autofocus: true, style: 'height:100px' %>
    </div>
    <div id="form-submit">
      <%= f.submit('Add project', class: 'submit-btn') %>
    </div>

  <% end %>
</div>

这是我的_projects.html.erb

<div id="name-container">
  <%= link_to "#{@projects.name}", project %>
</div>

这是我的routes.rb

Rails.application.routes.draw do
  get 'projects/index'

  get 'projects/show'

  get 'projects/edit'

  get 'projects/new'

  get 'welcome/index'

  get 'welcome/about'

  resources :projects

  devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

  resources :users do
    delete 'users/sign_out' => "devise/sessions#destroy"
  end

  authenticated :users do
    root to: 'projects#index', as: :authenticated_root
  end

  root 'welcome#index'

end

development.log 中的日志:

Started GET "/projects/new" for ::1 at 2016-10-12 16:40:01 +1100
Processing by ProjectsController#new as HTML
  Rendered projects/_form.html.erb (17.2ms)
  Rendered projects/new.html.erb within layouts/application (20.7ms)
  [1m[36mUser Load (0.1ms)[0m  [1mSELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1[0m  [["id", 3]]
Completed 200 OK in 74ms (Views: 72.7ms | ActiveRecord: 0.1ms)


Started GET "/projects/new" for ::1 at 2016-10-12 17:10:20 +1100
Processing by ProjectsController#new as HTML
  Rendered projects/_form.html.erb (1.7ms)
  Rendered projects/new.html.erb within layouts/application (2.7ms)
  [1m[35mUser Load (0.2ms)[0m  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 3]]
Completed 200 OK in 62ms (Views: 51.2ms | ActiveRecord: 1.0ms)


Started GET "/assets/application.self-64b323fda93125b3149a63eb79976d7aca18fdd68d693ad1ef615b0ab4a62cd2.css?body=1" for ::1 at 2016-10-12 17:10:20 +1100


Started GET "/assets/jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1" for ::1 at 2016-10-12 17:10:20 +1100


Started GET "/assets/jquery_ujs.self-784a997f6726036b1993eb2217c9cb558e1cbb801c6da88105588c56f13b466a.js?body=1" for ::1 at 2016-10-12 17:10:20 +1100


Started GET "/assets/menu.self-5ad0c1c387d3f54736adf9854f4fd424128328e7958f16a7e3f9c96cd1a85c41.js?body=1" for ::1 at 2016-10-12 17:10:20 +1100


Started GET "/assets/angular/angular.self-7f8df3e3ebe7623e233b951726e7da238883fa9e7a98b987ac7aecccf5f00510.js?body=1" for ::1 at 2016-10-12 17:10:20 +1100


Started GET "/assets/projects.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" for ::1 at 2016-10-12 17:10:20 +1100


Started GET "/assets/turbolinks.self-c5acd7a204f5f25ce7a1d8a0e4d92e28d34c9e2df2c7371cd7af88e147e4ad82.js?body=1" for ::1 at 2016-10-12 17:10:20 +1100


Started GET "/assets/users.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" for ::1 at 2016-10-12 17:10:20 +1100


Started GET "/assets/users/omniauth_callbacks.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" for ::1 at 2016-10-12 17:10:20 +1100


Started GET "/assets/welcome.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" for ::1 at 2016-10-12 17:10:20 +1100


Started GET "/assets/application.self-f8806224e027f3e3f0138ea9ce99319e298dfdb323304d1f1be6eae8e8c74724.js?body=1" for ::1 at 2016-10-12 17:10:20 +1100


Started POST "/projects" for ::1 at 2016-10-12 17:10:22 +1100
Processing by ProjectsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"RVkKamdrg/DM2rmDxclNqt0hurD29Fvg1TECS46y/PCAXJ4mn2cyus16IvOikPLpO+cyto99X2jH6vX8JZO/pQ==", "project"=>{"name"=>"", "deadline"=>"", "description"=>""}, "commit"=>"Add project"}
Unpermitted parameter: description
  [1m[36m (0.1ms)[0m  [1mbegin transaction[0m
  [1m[35m (0.0ms)[0m  rollback transaction
Redirected to http://localhost:3000/projects/index
Completed 302 Found in 4ms (ActiveRecord: 0.1ms)


Started GET "/projects/index" for ::1 at 2016-10-12 17:10:22 +1100
Processing by ProjectsController#index as HTML
  Rendered projects/index.html.erb within layouts/application (0.1ms)
  [1m[36mUser Load (0.1ms)[0m  [1mSELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1[0m  [["id", 3]]
Completed 200 OK in 23ms (Views: 22.8ms | ActiveRecord: 0.1ms)


Started GET "/projects/index" for ::1 at 2016-10-12 17:11:29 +1100
Processing by ProjectsController#index as HTML
  Rendered projects/index.html.erb within layouts/application (0.2ms)
  [1m[35mUser Load (0.1ms)[0m  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 3]]
Completed 200 OK in 39ms (Views: 35.3ms | ActiveRecord: 0.6ms)


Started GET "/assets/jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1" for ::1 at 2016-10-12 17:11:29 +1100


Started GET "/assets/turbolinks.self-c5acd7a204f5f25ce7a1d8a0e4d92e28d34c9e2df2c7371cd7af88e147e4ad82.js?body=1" for ::1 at 2016-10-12 17:11:29 +1100


Started GET "/assets/jquery_ujs.self-784a997f6726036b1993eb2217c9cb558e1cbb801c6da88105588c56f13b466a.js?body=1" for ::1 at 2016-10-12 17:11:30 +1100


Started GET "/assets/angular/angular.self-7f8df3e3ebe7623e233b951726e7da238883fa9e7a98b987ac7aecccf5f00510.js?body=1" for ::1 at 2016-10-12 17:11:30 +1100


Started GET "/assets/menu.self-5ad0c1c387d3f54736adf9854f4fd424128328e7958f16a7e3f9c96cd1a85c41.js?body=1" for ::1 at 2016-10-12 17:11:30 +1100


Started GET "/assets/application.self-64b323fda93125b3149a63eb79976d7aca18fdd68d693ad1ef615b0ab4a62cd2.css?body=1" for ::1 at 2016-10-12 17:11:30 +1100


Started GET "/assets/projects.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" for ::1 at 2016-10-12 17:11:30 +1100


Started GET "/assets/users.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" for ::1 at 2016-10-12 17:11:30 +1100


Started GET "/assets/users/omniauth_callbacks.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" for ::1 at 2016-10-12 17:11:30 +1100


Started GET "/assets/welcome.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" for ::1 at 2016-10-12 17:11:30 +1100


Started GET "/assets/application.self-f8806224e027f3e3f0138ea9ce99319e298dfdb323304d1f1be6eae8e8c74724.js?body=1" for ::1 at 2016-10-12 17:11:30 +1100


Started GET "/projects/new" for ::1 at 2016-10-12 17:11:31 +1100
Processing by ProjectsController#new as HTML
  Rendered projects/_form.html.erb (3.2ms)
  Rendered projects/new.html.erb within layouts/application (14.4ms)
  [1m[36mUser Load (0.1ms)[0m  [1mSELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1[0m  [["id", 3]]
Completed 200 OK in 57ms (Views: 51.8ms | ActiveRecord: 0.3ms)


Started POST "/projects" for ::1 at 2016-10-12 17:11:33 +1100
Processing by ProjectsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"s9h2ktiveACV3Mq4JJsz1/Z3eE5R+wlejmikisQI1BJ23eLeIKPJSpR8UchDwoyUELHwSChyDdacs1M9bymXRw==", "project"=>{"name"=>"", "deadline"=>"", "description"=>""}, "commit"=>"Add project"}
Unpermitted parameter: description
  [1m[35m (0.1ms)[0m  begin transaction
  [1m[36m (0.1ms)[0m  [1mrollback transaction[0m
Redirected to http://localhost:3000/projects/index
Completed 302 Found in 5ms (ActiveRecord: 0.2ms)


Started GET "/projects/index" for ::1 at 2016-10-12 17:11:33 +1100
Processing by ProjectsController#index as HTML
  Rendered projects/index.html.erb within layouts/application (0.2ms)
  [1m[35mUser Load (0.1ms)[0m  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 3]]
Completed 200 OK in 23ms (Views: 21.6ms | ActiveRecord: 0.1ms)

我之前已经做过一个类似的项目,这个问题从未发生过。这就是为什么我不明白。我当然想念一些东西。

希望你能弄明白 :) 我还在努力……

【问题讨论】:

  • 你得到了什么错误..!
  • 是的,错误信息是什么?并且您提到“但是当我提交表单时,它会将我重定向到项目索引(这就是我想要的)”。但是根据您的创建操作,它会在出现错误时重定向到index,否则重定向到show页面所以如果你想重定向到index然后也改变它
  • 创建动作中控制器内部的错误。 flash[:alert] = 'An error occurred adding your project. Please try again later 。因为当我单击添加项目以提交表单时,它会将我重定向到索引。但出现的不是项目,而是错误消息。
  • @sajan 我也试过了......但它也不起作用:/这就是我不明白的原因
  • @JustineDupuis 如果您也提供日志,我们可以进行调试。

标签: ruby-on-rails ruby routes crud


【解决方案1】:

如果您使用的是 Rails 4 及更高版本,则不应将attr_accessor 用于数据库中持久化的属性。这可能会导致这些属性无法正确保存。

尝试删除您的 attr_accessor 代码行并改用强参数:

def project_params
  params.require(:project).permit(:name, :deadline, :description)
end

在你的代码中你忘记了:description

此外,您的创建操作的设置方式可以防止您看到任何验证错误。相反,您创建的动作看起来像这样,原始表单在保存失败时重新呈现:

def create
    @project = Project.new(project_params)
    if @project.save
      redirect_to projects_show_path, notice: 'Project successfully added'
    else
      flash[:alert] = 'An error ocurred adding your project. Please try again later'
      render 'new'
    end
end

您还必须确保在表单的视图中显示错误消息。像这样:

<% if @project.errors.any? %>
  <ul>
    <% @project.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
  </ul>
<% end %>

【讨论】:

  • 另外,您的验证应仅限于您的模型...我看到您的控制器中有 validates :name 等...
  • 您的创建操作已设置,因此您看不到任何验证错误...可能是重定向的问题和原因
  • 我该如何改变?
  • 查看编辑后的答案。因为您要求所有字段都存在,所以您必须确保填写表单的所有字段
  • 谢谢。它有一点帮助。但是现在错误消息说截止日期不能为空:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多