我是一個新手,你可能會從我的文件中看到,我不完全知道我在做什麼:(。我有一個用戶和一個項目模型,一個項目屬於一個用戶,一個用戶有很多項目 我已經創建了一個用戶資料頁面,顯示屬於該用戶的所有項目,我想給用戶一個機會編輯或刪除他的個人資料直接刪除項目目前刪除似乎工作和編輯只適用於用戶第一次點擊「顯示」,然後編輯項目,但當用戶點擊編輯他的個人資料,項目爲被編輯是不是一個點擊。Rails:當我點擊編輯在我的部分它總是編輯相同的編號
這是我的個人資料視圖(views/users/show):
<% provide(:title, @user.name) %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<h1>
<%= @user.name %>
</h1>
</section>
</aside>
<div class="col-md-8">
<% if @user.projects.any? %>
<h3>Projects (<%= @user.projects.count %>)</h3>
<ol class="projects">
<%= render @projects %>
</ol>
<%= will_paginate @projects %>
<% end %>
</div>
</div>
這是_project部分與編輯聯繫:
<li id="project-<%= project.id %>">
<span class="title">
Title: <%= project.title %></span><br>
<span class="status">
<span class="user">
Submitted By: <%= link_to project.user.name, project.user %></span><br>
<span class="description">
Description: <%= project.description %></span><br>
<span class="status">
Status:<%= project.status %></span><br>
<span class="p_type">
Type: <%= project.p_type %></span><br>
<span class="timestamp">
Updated: <%= time_ago_in_words(project.updated_at) %> ago.
<% if current_user?(project.user) %>
<%= link_to "delete", project, method: :delete,
data: { confirm: "You sure?" } %>
<%= link_to "edit", edit_project_path %>
<%= link_to "New Project!", new_project_path %>
<% end %>
<!-- <%= link_to "show", project, method: :get %> -->
</span>
</li>
這是我的項目控制器:
class ProjectsController < ApplicationController
before_action :logged_in_user, only: [:create, :edit, :destroy]
before_action :correct_user, only: [:edit, :destroy]
def index
@projects = Project.paginate(page: params[:page])
end
def show
@project = Project.find(params[:id])
end
def new
@project = Project.new
end
def create
@project = current_user.projects.build(project_params)
if @project.save
flash[:success] = "Project created!"
redirect_to root_url
else
@feed_items = []
render 'new'
end
end
def edit
@project = Project.find params[:id]
end
def update
@project = Project.find params[:id]
@user = User.find(params[:id])
if @project.update(project_params)
flash[:success] = "Project updated"
redirect_to user_path(@user)
else
render 'edit'
end
end
def destroy
@project.destroy
flash[:success] = "Project deleted"
redirect_to request.referrer || root_url
end
private
def project_params
params.require(:project).permit(:description, :title, :status, :p_type)
end
def correct_user
@project = current_user.projects.find_by(id: params[:id])
redirect_to root_url if @project.nil?
end
end
我的項目模型:
class Project < ApplicationRecord
belongs_to :user
default_scope -> { order(updated_at: :desc) }
validates :user_id, presence: true
validates :description, presence: true, length: { maximum: 250 }
validates :title, presence: true, length: { maximum: 60 }
validates :p_type, presence: true
validates :status, presence: true
end
我的路線文件:
Rails.application.routes.draw do
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :projects
end
最後,如果有幫助,當我做編輯操作,這是跟蹤:
Started GET "/projects/1/edit" for 86.44.57.230 at 2016-10-29 12:16:49 +0000
Cannot render console from 86.44.57.230! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ProjectsController#edit as HTML
Parameters: {"id"=>"1"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Project Load (0.2ms) SELECT "projects".* FROM "projects" WHERE "projects"."user_id" = ? AND "projects"."id" = ? ORDER BY "projects"."updated_at" DESC LIMIT ? [["user_id", 1], ["id", 1], ["LIMIT", 1]]
Project Load (0.2ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = ? ORDER BY "projects"."updated_at" DESC LIMIT ? [["id", 1], ["LIMIT", 1]]
Rendering projects/edit.html.erb within layouts/application
Rendered shared/_error_messages.html.erb (0.6ms)
Rendered projects/edit.html.erb within layouts/application (24.9ms)
Rendered layouts/_shim.html.erb (0.4ms)
Rendered layouts/_header.html.erb (1.0ms)
Rendered layouts/_footer.html.erb (0.4ms)
Completed 200 OK in 179ms (Views: 172.4ms | ActiveRecord: 0.7ms)
也增加了編輯表單
<% provide(:title, "Edit Project") %>
<h1>Update your Project</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@project) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control' %>
<%= f.label :p_type %>
<%= f.select :p_type, ([['QuickWin', 'QuickWin'],
['Project', 'Project']]) %>
<%= f.label :status %>
<%= f.select :status, ([['Not Yet Started', 'Not Yet Started'],
['In Progress', 'In Progress'], ['In Testing', 'In Testing'],
['On Hold', 'On Hold'], ['Launched', 'Launched']]) %>
<div class="field">
<%= f.text_area :description, placeholder: "Briefly summarise the purpose of this project..." %>
</div>
<%= f.submit "Save changes", class: "btn btn-primary" %>
<% end %>
</div>
</div>
預先感謝您的幫助!
東西真的就在這裏。你自己定義了一個'edit_project_path'方法嗎?由rails定義的路由幫助程序應該提供記錄或id。該調用應該看起來像'edit_project_path(@record)' – max
'謝謝你看我的代碼。如在edit_project_path(@project)中一樣?這不適用於我,它只適用於edit_project_path(項目),但是當它返回到用戶頁面時('user = User.find(params [:id])'行''def'在項目控制器中更新'方法) – Alessia