best_in_place can't edit users attributes in profile#show view | rails 4.0 & devise

351 Views Asked by At

I'm a rails begginer, having some issues with best_in_place gem. I have a profile controller with a show view, nothing complicated here:

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

I want my users to be able to edit their profile directly from their profile page. SO in my profile#show view I have this field:

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

The problem is that I get this error when I try to load the show profile page:

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

I dont know how I'm supposed to use best_in_place with a profile resource in my routes. here's my rake routes for the profile resources:

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

thanks in advance for your answers !


Update:

I think that best_in_place tries to get to the user_path so I tried to change <%= best_in_place @user, :telephone, :type => :input %> to: <%= best_in_place profile_path(@user), :telephone, :type => :input %> but now I get the following error:

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

There are 2 best solutions below

1
On BEST ANSWER

You have

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

Change to

  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

You can pass :url option to best_in_place, by default it goes objects path. Your solution should be

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

Found the answer ! As I wrote in the update best_in_place tries to access to the user_path, I only needed to add a path option:

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