如何在 rails ruby 中更新和编辑一对多关系 MVC?

How to update and edit One to Many relationship MVC in ruby on rails?

我有一个简单的问题,我只需要编辑和删除rails中的一对多关系 在我的例子中 Status 属于 userUser 有很多 statuses

这是我试过的

状态中

edit.html.erb

<h1>Edit Status of doctor <%= @user.name %></h1>

<%= form_for @status,:url => {:action => :update, :id => @user.id} do |f| %>
  status: (Received, Processed, Shipped) <%= f.text_field :name %><br>
  <%= f.submit %>
<% end %>

I have this error undefined method `name' for nil:NilClass

身份控制器

class StatusesController < ApplicationController
  def new
    @user =User.find(params[:id])
    @status = @user.statuses.build
  end

  def create
    @user =User.find(params[:id])
    @status = @user.statuses.build(status_params)
    if @status.save
      redirect_to new_status_path(:id => @user.id)
    end
  end

  def edit
  end

  def update
    @user =User.find(params[:id])
    if @user.statuses.update(status_params)
      redirect_to root_path, notice: "user Information updated successfully"
    else
      flash.now[:error] = "Couldn't update!"
      render :edit
    end
  end
  private

  def status_params
    params.require(:status).permit(:name)
  end
end

用户show.html.erb

 <%= link_to @user.name.capitalize, :controller => :statuses, :action => :new, :id => @user.id %><br>
 <% if !@user.statuses.blank? %>
   <% for item in @user.statuses %>
     <strong>Status:</strong> <%= item.name %>
     <%= link_to "Edit", edit_status_path(item),class: "btn btn-success" %><br>
   <% end %>
 <% else %>
   No status yet
 <% end %>

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_many :statuses
end

status.rb

class Status < ActiveRecord::Base
  belongs_to :user
end

您没有在您的控制器中设置变量@user,请使用 before 操作来保持您的 DRY 原则。

class StatusesController < ApplicationController
  before_action :set_user

  def new
    @status = @user.statuses.build
  end

  def create
    @status = @user.statuses.build(status_params)
    if @status.save
      redirect_to new_status_path(:id => @user.id)
    end
  end

  def edit
  end

  def update
    if @user.statuses.update(status_params)
      redirect_to root_path, notice: "user Information updated successfully"
    else
      flash.now[:error] = "Couldn't update!"
      render :edit
    end
  end

private

  def set_user
    @user = User.find(params[:id])
  end

  def status_params
    params.require(:status).permit(:name)
  end
end

要删除该用户的所有关联记录,请放入您的文件user.rb

has_many :statuses, dependent: :destroy

我这样解决了

   class StatusesController < ApplicationController
      before_action :authenticate_user!,except: [:index,:show]
      before_action :find_status ,only:[:show,:update,:edit,:destroy]
    
       def new
        @user =User.find(params[:id])
        @status = @user.statuses.build
      end
    
      def create
        @user =User.find(params[:id])
        @status = @user.statuses.build(status_params)
        if @status.save
          redirect_to doctor_path(:id => @user.id)
        end
      end
    
      def edit
      end
    
      def update
    
        if @status.update(status_params)
          redirect_to root_path, alert: "user Information updated successfully"
        else
          flash.now[:error] = "Couldn't update!"
          render :edit
        end
      end
      def destroy
        @status.destroy
        redirect_to doctors_path, notice: "Status deleted Successfully"
      end 
    
    private
    
      def find_status
        @status = Status.find(params[:id])
      end
    
      def status_params
        params.require(:status).permit(:name)
      end

end

状态编辑文件中

<%= form_for @status do |f| %>
  status: (Received, Processed, Shipped) <%= f.text_field :name %><br>
  <%= f.submit %>
  <% end %>

在用户索引中显示

<% if !@user.statuses.blank? %>
      <% for item in @user.statuses %>
        <strong>Status:</strong> <%= item.name %>
        <% if current_user && current_user.admin? %>
        <%= link_to "Edit", edit_status_path(item),class: "btn btn-success" %>
        <%= link_to "Delete", item, method: :delete, data: {confirm: "Are you sure?"},class: "btn btn-danger" %><br>
        <% end %>
      <% end %>
    <% else %>
      No status yet
    <% end %>