Rails:当我点击部分编辑时,它总是编辑相同的ID

时间:2016-10-29 12:19:06

标签: ruby-on-rails

我是新手,你可能会从我的文件中看到,我不完全知道我在做什么:(。目前我有一个用户和一个项目模型。一个项目属于一个用户,用户有很多项目。 我创建了一个用户个人资料页面,显示属于该用户的所有项目。我想让用户有机会直接从他的个人资料中编辑或删除项目。目前,删除似乎有效,只有当用户首次点击“显示”然后编辑项目时,“编辑”才有效。但是当用户从他的个人资料中点击编辑时,要编辑的项目不是单击的项目。

这是我的个人资料视图(views / users / show):

BtnNor.Click += new EventHandler(NorChart); //BtnNor is the name of the button
}
void NorChart(object sender, EventArgs e) 
{
    SingleChart Form_SC = new SingleChart(); //SingleChart is the name of the second form.
    Form_SC.label1.text = "2nd form label value" //This will change label 2nd form.
    Form_SC.Show();

}

这是带有编辑链接的_project部分:

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

这是我的项目控制员:

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

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

在下面的行中缺少您想要更改的记录:

<%= link_to "edit", edit_project_path %> 

将'edit_project_path'添加到项目参考:

<%= link_to 'edit', edit_project_path(project) %>

我希望这对你有所帮助。

...

关于错误“无法找到有'id'= 424的用户”,请致电@user = User.find(params [:id])...

当你点击提交时,你会在控制器projects_controller.rb上发送一个关于方法更新的PUT操作,并使用带有Project对象的params。

在你的projects_controller.rb中,你的方法'update'会收到一个Project对象。

此处参数必须显示为

params = { ... project:{:title => '...' , :ptype => ..}, 'id' => XXX }

这个'id'是project.id !!

projects_controller.rb文件

def update
  @project = Project.find params[:id]
  @user = User.find(params[:id])  <= you try to find a User with project.id

我认为您希望用户加入此项目。

@user = @project.user
相关问题