best_in_place无法在个人资料#show view |中编辑用户属性rails 4.0&设计

时间:2014-12-12 16:50:10

标签: ruby-on-rails ruby ruby-on-rails-4 devise best-in-place

我是一名铁路运营商,在best_in_place gem方面存在一些问题。我有一个带有显示视图的配置文件控制器,这里没什么复杂的:

class ProfileController < ApplicationController
  before_action :get_user

  def show
  end

  def edit
  end

  def update
    if @user.update(user_params)
      format.html { redirect_to show_profile_path(@user) }
    else
      format.html { render :edit }
    end
  end

  private

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

  def user_params
    params.require(:user).permit(:first_name, :last_name, :address, :lat, :lng, :telephone)
  end
end

我希望我的用户能够直接从他们的个人资料页面编辑他们的个人资料。在我的个人资料中#show view我有这个字段:

    <em> <%= best_in_place @user, :telephone, :type => :input %> </em><br>

问题是我在尝试加载节目简介页面时收到此错误:

undefined method `user_path' for #<#<Class:0x00000002d04880>:0x00000008f85b30>

我不知道我应该如何在我的路线中使用best_in_place和个人资料资源。这是我的个人资料资源的佣金路线:

profile_index GET      /profile(.:format)                                 profile#index
                         POST     /profile(.:format)                                 profile#create
             new_profile GET      /profile/new(.:format)                             profile#new
            edit_profile GET      /profile/:id/edit(.:format)                        profile#edit
                 profile GET      /profile/:id(.:format)                             profile#show
                         PATCH    /profile/:id(.:format)                             profile#update
                         PUT      /profile/:id(.:format)                             profile#update
                         DELETE   /profile/:id(.:format)                             profile#destroy

提前感谢您的回答!


更新

我认为best_in_place尝试访问user_path,因此我尝试将<%= best_in_place @user, :telephone, :type => :input %>更改为: <%= best_in_place profile_path(@user), :telephone, :type => :input %>但现在我收到以下错误:

 undefined method `telephone' for "/profile/1":String`

2 个答案:

答案 0 :(得分:1)

你有

  def update
    if @user.update(user_params)
      format.html { redirect_to show_profile_path(@user) }
    else
      format.html { render :edit }
    end
  end

更改为

  def update
    if @user.update(user_params)
      format.html { redirect_to profile_path(@user) }
      # when you have run rake routes you saw profile as route name for show
    else
      format.html { render :edit }
    end
  end

您可以将:url选项传递给best_in_place,默认情况下它会转到对象路径。你的解决方案应该是

<em> <%= best_in_place @user, :telephone, :type => :input, :url => profile_url(@user) %> </em><br>

答案 1 :(得分:1)

找到答案!正如我在更新中写的那样,best_in_place尝试访问user_path,我只需要添加一个路径选项:

<%= best_in_place @user, :telephone, type: :input, path: profile_path %>