如何从datagrid gem链接到记录?

时间:2014-06-13 18:13:11

标签: ruby-on-rails-3 datagrid

我是ruby的新手,现在我遇到了以下问题:我安装了datagrid gem并创建了一个范围我的User模型的网格。我还在网格中添加了一个Actions-Column来编辑每个用户。当我点击编辑链接时,我收到一条错误消息“#34;无法找到没有ID的用户"引用users_controller。

users_grid.rb:

class UsersGrid

include Datagrid

scope do
  User
end

filter(:id, :integer, :header => "test")
filter(:created_at, :date, :range => true)

column(:id)
column(:username)
column(:forename)
column(:lastname)
column(:email) do |model|
  format(model.email) do |value|
    link_to value, "mailto:#{value}"
  end
end
column(:created_at) do |model|
  model.created_at.to_date
end
column(:actions, :html => true) do |record|
  link_to "Edit", edit_path
end
end

users_controller.rb:

class UsersController < ApplicationController

  before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
  before_action :correct_user,   only: [:edit, :update]

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

  def new
     @user = User.new
  end

  def index
    @users = User.paginate(page: params[:page])
    @grid = UsersGrid.new(params[:users_grid])
    @assets = @grid.assets.paginate(:page => params[:page], :per_page => 15)
  end

  def create
    @user = User.new(user_params)
    if @user.save
      login @user
      flash[:success] = "User created successfully!"
      redirect_to @user
    else
      render 'new'
    end
  end

  def edit
  end

  def update
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User deleted."
    redirect_to users_url
  end

  private

    def user_params
      params.require(:user).permit(:username, :forename, :lastname, :email, :password,
                               :password_confirmation)
    end

    # Before filters

    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end
end

routes.rb中:

resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :dashboards, only: [:index]
root  'dashboards#operation'
match '/index', to: 'users#index', via: 'get'
match '/edit', to: 'users#edit', via: 'get'
match '/login', to: 'sessions#new', via: 'get'
match '/logout', to: 'sessions#destroy', via: 'delete'
match '/operation',  to: 'dashboards#operation', via: 'get'

错误消息:

  

无法找到没有ID的用户

users_controller中的相关代码:

def correct_user
  @user = User.find(params[:id])
  redirect_to(root_url) unless current_user?(@user)
end 

感谢您的帮助......我的知识在红宝石中太有限了。

3 个答案:

答案 0 :(得分:1)

游戏也迟到了,但这是怎么做的。在您的users_grid.rb中:

  column(:username, html: true) do |record|
    link_to record.username, edit_user_path(record)
  end

当然,如果你想要只有一个名为edit的列,你可以这样做:

  column(:username, html: true) do |record|
    link_to "Edit", edit_user_path(record)
  end

答案 1 :(得分:0)

聚会很晚,但我遇到了一个类似的问题,它与ID参数在URL中(或从中丢失)有关。我会检查你的路线。

答案 2 :(得分:0)

试试这个:

link_to "Edit", edit_path(record)