Rails - 链接以编辑用户配置文件中的另一个模型

时间:2016-02-03 14:19:33

标签: ruby-on-rails ruby

我有一个rails应用程序,用户可以列出和编辑有关他们拥有的车辆的信息。用户拥有车辆,车辆属于用户。

在用户个人资料中,有一个用于创建新车辆的表单。

我可以使用删除链接在用户个人资料中显示自有车辆列表,但是当我使用edit_vehicle_path添加指向编辑操作的链接时,我只会获得指向该车辆的链接ID等于用户ID。

users_controller.rb如下所示:

class UsersController < ApplicationController

  before_action :correct_user, only: [:show]

  def show
    @user = User.find(params[:id])
    @vehicles = @user.vehicles
    @vehicle = current_user.vehicles.build if user_signed_in?
  end

  private

    # before filters

    # Each user can access only his own profile
    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user == @user
    end

end

vehicles_controller.rb看起来像这样:

class VehiclesController < ApplicationController
  before_action :authenticate_user!,  only: [:create, :destroy]
  before_action :correct_user,        only: [:destroy, :edit, :update]

  def create
    @vehicle = current_user.vehicles.build(vehicle_params)
    if @vehicle.save
      flash[:success] = "New vehicle added!"
      redirect_to current_user
    else
      flash[:danger] = "Houston, we got a problem!"
      render current_user
    end
  end

  def edit
    # @vehicle is already defined in correct_user which is called before_action
  end

  def update
    # @vehicle is already defined in correct_user which is called before_action
    if @vehicle.update_attributes(vehicle_params)
      flash[:success] = "Vehicle updated"
      redirect_to current_user
    else
      render 'edit'
    end
  end

  def destroy
    @vehicle.destroy
    flash[:success] = "Vehicle removed"
    redirect_to request.referrer || root_url
  end

  private

    def vehicle_params
      params.require(:vehicle).permit(:name, :matriculation_date)
    end

    # before filters

    # Each user can remove/edit/destroy only the vehicles he owns
    def correct_user
      @vehicle = current_user.vehicles.find_by(id: params[:id])
      redirect_to root_url if @vehicle.nil?
    end

end

routes.rb中:

Rails.application.routes.draw do

  devise_for :users
  root 'static_pages#home'
  resources :users,               only: [:show, :index]
  resources :vehicles,            only: [:create, :edit, :update, :destroy]

end

我用来显示单车信息的部分是:

<li id="micropost-<%= vehicle.id %>">
  <span class="name"><%= vehicle.name %></span>
  <span class="matriculation"><%= vehicle.matriculation_date %></span>
  <span class="id"> - id: <%= vehicle.id %></span>
  <%= link_to '<i class="fa fa-cog"></i> edit'.html_safe, edit_vehicle_path %>
  <%= link_to '<i class="fa fa-trash"></i> delete'.html_safe, vehicle, method: :delete, data: { confirm: "You sure?" } %>
</li>

并且完整的用户/节目视图是这样的:

<%= form_for(@vehicle) do |f| %>

    <%= f.label :name, "Vehicle name" %>
    <%= f.text_field :name, class: 'form-control' %>

    <%= f.label :matriculation_date %>
    <%= f.date_field :matriculation_date, class: 'form-control' %>

    <%= f.submit "Add", class: "btn btn-primary" %>
<% end %>

<h3>User email: <%= @user.email %></h3>

<% if @user.vehicles.any? %>
    <h3>Vehicles (<%= @user.vehicles.count %>)</h3>
    <ol>
      <%= render @vehicles %>
    </ol>
<% end %>

感谢

2 个答案:

答案 0 :(得分:2)

edit_vehicle_path需要知道vehicle才能建立正确的链接。将其更改为:

<%= link_to '<i class="fa fa-cog"></i> edit'.html_safe, edit_vehicle_path(vehicle) %>

答案 1 :(得分:2)

从我的观点来看,最好使用nested Routes

resources :users, only: [:show, :index] do
  resources :vehicles, only: [:create, :edit, :update, :destroy]
end

请注意,这也会改变您的路由助手和路径,但更清楚的是,您编辑了用户车。

edit_user_vehicle_path(@user, @vehicle) 
# => /users/1/vehicles/2/edit