编辑个人资料按钮不起作用

时间:2018-04-07 23:19:38

标签: ruby-on-rails hyperlink routes url-routing

刚开始学习ruby,所以提前抱歉任何愚蠢的错误。无论如何,每当我尝试以“客户”身份登录时,以下是由于客户资料中的“更新”按钮而产生的错误。

cust_profile.rb

class CustProfile < ApplicationRecord
  belongs_to :user
end

user.rb

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  enum role: [:customer, :cook, :admin]
  # after_initialize :set_default_role, :if => :new_record?

  has_attached_file :photo

  validates_attachment_presence :photo
  validates_attachment_size :photo, :less_than => 1.megabytes
  validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']

  def set_default_role
    self.role ||= :customer
  end

  has_one :cook_profile, required: false
  has_one :cust_profile, required: false

end

cust_home.html

<h1>Customer Profile</h1>
<%= link_to 'Update',  edit_cust_profile_path(current_user.cust_profile) %>

<link href="http://fonts.googleapis.com/css?family=Open+Sans" rel='stylesheet' type='text/css'>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">

<div class="container" style="margin-top: 20px; margin-bottom: 20px;">
    <div class="row pannel">
        <div class="col-md-8 col-xs-12">
            <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRwQzkGr6zOpQD7nG8YtZIIzlutO7kOL1NkG88BOH5fNVBqkwWc" class="img-thumbnail picture hidden-xs" />
            <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRwQzkGr6zOpQD7nG8YtZIIzlutO7kOL1NkG88BOH5fNVBqkwWc" class="img-thumbnail visible-xs picture_mob" />
            <div class="header">
              <h2>Customer name</h2>
              <%= @user.cust_profile.name %>
              <h2>Location</h2>
            <%= @user.cust_profile.location %>
    <div class='bookings_buttons' style="float: left; margin-top: 75px">
        <button type="button" onclick="alert('No current bookings!')">View Current Booking</button>
        <button type="button" onclick="alert('No previous bookings!')">View Previous Bookings</button>
    </div>
    <div class='cook_buttons'>
        <button type="button" onclick="alert('This feature is not available yet!')">Book a cook</button>
        <button type="button" onclick="alert('This feature is not available yet!')">Rate a cook</button>
    </div>

cust_profile.html

<div class="page-header text-center">
  <h1>My Profile</h1>
  <%= link_to 'Back to Home Page',  root_path %>
</div>

<h2>User Credentials</h2>
<table class="table">
  <tr>
    <th>Photo</th>
    <th>Email</th>
    <th>Password</th>
    <th></th>
  </tr>
  <tr>
    <td><%= image_tag @user.photo.url, :size => "100x100" %></td>
    <td><%= @user.email %></td>
    <td><%= @user.encrypted_password %></td>
    <td><%= link_to 'Update',  edit_user_registration_path %></td>
  </tr>
</table>


<% if (@user.cust_profile == nil) then %>
<%= link_to 'Create Profile',  new_cust_profile_path, :class => "btn btn-primary" %>
<br><br>
<% else %>
<table class="table">
  <tr>
    <th>Name</th>
    <th>Location</th>
    <th></th>
  </tr>
  <tr>
    <td><%= @user.cust_profile.name %></td>
    <td><%= @user.cust_profile.location %></td>
    <td><%= link_to 'Update',  edit_cust_profile_path(@user.cust_profile) %></td>
  </tr>
</table>
<% end if %>

cust_profiles_controller.rb

class CustProfilesController < ApplicationController
  before_action :set_cust_profile, only: [:show, :edit, :update, :destroy]

  # GET /cust_profiles
  # GET /cust_profiles.json
  def index
    @cust_profiles = CustProfile.all
  end

  # GET /cust_profiles/1
  # GET /cust_profiles/1.json
  def show
    @cust_profile = CustProfile.find(params[:id])
  end

  # GET /cust_profiles/new
  def new
    @cust_profile = CustProfile.new
  end

  # GET /cust_profiles/1/edit
  def edit
    @cust_profile = CustProfile.find(params[:id])
  end

  # POST /cust_profiles
  # POST /cust_profiles.json
  def create
   @cust_profile = CustProfile.new(cust_profile_params)
   @cust_profile.user_id = current_user.id
    respond_to do |format|
      if @cust_profile.save
        format.html { redirect_to @cust_profile, notice: 'Customer profile was successfully created.' }
        format.json { render :show, status: :created, location: @cust_profile }
      else
        format.html { render :new }
        format.json { render json: @cust_profile.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /cust_profiles/1
  # PATCH/PUT /cust_profiles/1.json
  def update
    @cust_profile = CustProfile.find(params[:id])

    respond_to do |format|
      if @cust_profile.update(cust_profile_params)
        format.html { redirect_to @cust_profile, notice: 'Customer profile was successfully updated.' }
        format.json { render :show, status: :ok, location: @cust_profile }
      else
        format.html { render :edit }
        format.json { render json: @cust_profile.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /cust_profiles/1
  # DELETE /cust_profiles/1.json
  def destroy
    @cust_profile = CustProfile.find(params[:id])

    @cust_profile.destroy
    respond_to do |format|
      format.html { redirect_to cust_profiles_url, notice: 'Customer profile was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.

    def set_profile
      @cust_profile = current_user.cust_profile
    end

    def set_cust_profile
      @cust_profile = CustProfile.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def cust_profile_params
      params.require(:cust_profile).permit(:name, :location)
    end
end

easy_controller.rb

class EasyController < ApplicationController
  def home
    if user_signed_in?
      @user = current_user

      if current_user.cook?
        render :cook_home
      end
      if current_user.customer?
        render :cust_home
      end
      if current_user.admin?
        render :admin_home
      end
    end        
  end

  def profile
    if user_signed_in?
      @user = current_user

      if current_user.cook?
        render :cook_profile
      else if current_user.cust?
    render :cust_profile
    end
    end
  end

  def cust_home
      @user = current_user
      if current_user.cust?
          render :cust_home
      end
  end
end
end

以下是因为我的cust_home.html页面中的“更新”按钮而生成的错误

Easy#home

中的ActionController :: UrlGenerationError

没有路由匹配{:action =&gt;“edit”,:controller =&gt;“cust_profiles”,:id =&gt; nil},缺少必需的密钥:[:id]

我怀疑错误来自我的控制器,但无法弄清楚如何继续。

修改

的routes.rb

Rails.application.routes.draw do
  resources :cuisines
  resources :cook_profiles
  resources :cust_profiles
  devise_for :users, :controllers => {:registrations => 'registrations'}
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

  #----- Home routes
  get 'home', to: 'easy#home', as: 'easy_home'
  get 'cook_home', to: 'easy#cook_home', as: 'cook_home'
  get 'cust_home', to: 'easy#cust_home', as: 'cust_home'
  get 'admin_home', to: 'easy#admin_home', as: 'admin_home'
  root to: 'easy#home'

  #----- Route to user-appropriate profile page
  get '/profile', to: 'easy#profile', as: 'profile'

end

1 个答案:

答案 0 :(得分:0)

错误不是由Update按钮引起的。它来自:

ActionController::UrlGenerationError in Easy#home 

因此,您必须检查EasyController#home的视图。当您查看它(cust_home.html)时,只有一个地方可能导致此问题:

<%= link_to 'Update',  edit_cust_profile_path(current_user.cust_profile) %>

因为它说:

No route matches {:action=>"edit", :controller=>"cust_profiles", :id=>nil}, missing required keys: [:id]

可能current_user.cust_profilenil