Create issue Ruby on Rails의 redirect_to 작업

justinedps26

사용자 인증을 사용하여 작은 Rails 앱을 구축하고 있습니다. 프로젝트를 구성하고, 작업을 추가하고, 댓글을 추가 할 수있는 앱입니다.

현재 사용자 인 경우 '프로젝트 시작'을 클릭하면 양식을 볼 수 있습니다. 그러나 양식을 제출하면 프로젝트 색인 (내가 원하는)으로 리디렉션되지만 프로젝트가 저장되지는 ​​않습니다. 대신 오류 메시지가 있습니다. 어쨌든 코드로 볼 수 있습니다.

여기 내 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)

나는 이미 이전에 이와 같은 프로젝트를 수행 했으며이 문제는 발생하지 않았습니다. 그것이 내가 이해하지 못하는 이유입니다. 나는 확실히 뭔가를 놓친다.

알아낼 수 있기를 바랍니다. :) 아직 작업 중입니다 ...

Ren

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 %>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Render and redirect_to in rails

분류에서Dev

Rails redirect_to not rendering

분류에서Dev

Rspec의 Redirect_to

분류에서Dev

Rails redirect_to choice page

분류에서Dev

Rails redirect_to with DELETE method

분류에서Dev

다른 컨트롤러의 redirect_to create.js.erb

분류에서Dev

Ruby on Rails Fields For Issue

분류에서Dev

웹 모의는 Rails 컨트롤러에서 redirect_to와 함께 작동하지 않습니다.

분류에서Dev

Rails redirect_to 부모 부분 /보기

분류에서Dev

Rails 4.1 nested routes redirect_to test won't pass

분류에서Dev

Rails: Erratic secondary post request after redirect_to action

분류에서Dev

인스턴스 변수에 대한 redirect_to는 Rails 4에서 작동하지 않습니다.

분류에서Dev

redirect_to 문제-레일의 루비

분류에서Dev

Ruby on Rails 4 : 모달의 새로운 작업

분류에서Dev

Ruby on Rails-하나의보기에서 여러 작업 수행

분류에서Dev

UsersController # create의 SyntaxError-@ user.save Ruby on Rails

분류에서Dev

Rails 4 : 'redirect_to current_thing'정의되지 않은 오류

분류에서Dev

Ruby on Rails,이 컨트롤러의 작업을 시작하는 URL 가져 오기

분류에서Dev

Ruby on Rails : 경로의 싱글 톤 클래스 작업에 대한 참조

분류에서Dev

Post의 모델에 대한 "재 게시"작업을 구현하는 Ruby on Rails

분류에서Dev

Ruby / Rails-Paperclip :: Error in ModificationsController # create

분류에서Dev

Rails 4 : redirect_to : back 및 매개 변수 변경 / 삭제

분류에서Dev

DELETE 메서드를 사용하는 Rails redirect_to

분류에서Dev

Rails redirect_to 내부 커스텀 클래스

분류에서Dev

ruby on rails, redirect if hitting back button in browser

분류에서Dev

ruby-http : // localhost : 3000 / admin / users를 실행하지만 redirect_to http : // localhost : 3000 / admin / login

분류에서Dev

Rails 4.x로 업그레이드 한 후 Rails 웹 서비스의 Ruby가 작동하지 않음

분류에서Dev

PHP redirect_to () 함수 정의되지 않음

분류에서Dev

Ruby on Rails : 작업 진행 중-connect (2)가 차단됨

Related 관련 기사

  1. 1

    Render and redirect_to in rails

  2. 2

    Rails redirect_to not rendering

  3. 3

    Rspec의 Redirect_to

  4. 4

    Rails redirect_to choice page

  5. 5

    Rails redirect_to with DELETE method

  6. 6

    다른 컨트롤러의 redirect_to create.js.erb

  7. 7

    Ruby on Rails Fields For Issue

  8. 8

    웹 모의는 Rails 컨트롤러에서 redirect_to와 함께 작동하지 않습니다.

  9. 9

    Rails redirect_to 부모 부분 /보기

  10. 10

    Rails 4.1 nested routes redirect_to test won't pass

  11. 11

    Rails: Erratic secondary post request after redirect_to action

  12. 12

    인스턴스 변수에 대한 redirect_to는 Rails 4에서 작동하지 않습니다.

  13. 13

    redirect_to 문제-레일의 루비

  14. 14

    Ruby on Rails 4 : 모달의 새로운 작업

  15. 15

    Ruby on Rails-하나의보기에서 여러 작업 수행

  16. 16

    UsersController # create의 SyntaxError-@ user.save Ruby on Rails

  17. 17

    Rails 4 : 'redirect_to current_thing'정의되지 않은 오류

  18. 18

    Ruby on Rails,이 컨트롤러의 작업을 시작하는 URL 가져 오기

  19. 19

    Ruby on Rails : 경로의 싱글 톤 클래스 작업에 대한 참조

  20. 20

    Post의 모델에 대한 "재 게시"작업을 구현하는 Ruby on Rails

  21. 21

    Ruby / Rails-Paperclip :: Error in ModificationsController # create

  22. 22

    Rails 4 : redirect_to : back 및 매개 변수 변경 / 삭제

  23. 23

    DELETE 메서드를 사용하는 Rails redirect_to

  24. 24

    Rails redirect_to 내부 커스텀 클래스

  25. 25

    ruby on rails, redirect if hitting back button in browser

  26. 26

    ruby-http : // localhost : 3000 / admin / users를 실행하지만 redirect_to http : // localhost : 3000 / admin / login

  27. 27

    Rails 4.x로 업그레이드 한 후 Rails 웹 서비스의 Ruby가 작동하지 않음

  28. 28

    PHP redirect_to () 함수 정의되지 않음

  29. 29

    Ruby on Rails : 작업 진행 중-connect (2)가 차단됨

뜨겁다태그

보관