如何在Rails上的Ruby中更新和编辑一对多关系MVC? [英] How to update and edit One to Many relationship MVC in ruby on rails?

查看:227
本文介绍了如何在Rails上的Ruby中更新和编辑一对多关系MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题,在我的情况下,我只需要编辑和删除rails
中的一对多关系状态属于用户用户具有许多状态

I have a simple question, all I need is to edit and delete one to many relationship in rails in my case Status belongs to user and User has many 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 %>



我对nil:NilClass有此错误的未定义方法'name'

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


推荐答案

我通过执行此操作来解决

I solve it by doing this

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

这篇关于如何在Rails上的Ruby中更新和编辑一对多关系MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆