【问题标题】:best_in_place can't edit users attributes in profile#show view | rails 4.0 & devisebest_in_place 无法在个人资料#show 视图中编辑用户属性 | rails 4.0 & 设计
【发布时间】:2014-12-12 16:50:10
【问题描述】:

我是一名 Rails 初学者,在使用 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 视图中我有这个字段:

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

问题是当我尝试加载显示配置文件页面时出现此错误:

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

我不知道我应该如何在我的路线中使用 best_in_place 和配置文件资源。这是我的个人资料资源的 rake 路线:

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,因此我尝试将 &lt;%= best_in_place @user, :telephone, :type =&gt; :input %&gt; 更改为: &lt;%= best_in_place profile_path(@user), :telephone, :type =&gt; :input %&gt; 但现在我收到以下错误:

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

【问题讨论】:

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


    【解决方案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>
    

    【讨论】:

    • 我还没有看到谢谢你的回答。但我仍然有同样的问题。我认为 best_in_place 试图获取 user_path。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-19
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多