RegistrationController中的ActionController :: UrlGenerationError #create

时间:2016-09-10 17:21:27

标签: ruby-on-rails ruby devise many-to-many

我建立一个rails应用程序是用户通过Devise注册的。

<h2>Sign up</h2>

<% resource.build_profile %>
 <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>

<div class="container">


     <%= f.fields_for :profile do |profile_form| %>
     <div class="field">
     <%= profile_form.label :company_name %><br />
      <%= profile_form.text_field :company_name %>
      </div>

      <div class="field">
     <%= profile_form.label :buisness_type %><br />
      <%= profile_form.text_field :buisness_type %>
      </div>

      <div class="field">
      <%= profile_form.label :contact_person %><br />
      <%= profile_form.text_field :contact_person %>
      </div>
    <% end %>

    <div class="field">
    <%= f.label :contact_person_email %><br />
    <%= f.email_field :email, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
  </div>

  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
</div>  

注册后,用户将被定向到views/profile/show.html.erb

注册后,rails会出现此错误:ActionController::UrlGenerationError in RegistrationsController#create

No route matches {:action=>"show", :controller=>"profile", :id=>nil, :user_id=>#<User id: 14, email: "fert@fhg.is", created_at: "2016-09-10 17:46:35", updated_at: "2016-09-10 17:46:35">} missing required keys: [:id] 这是views/devise/registrations/new.html.erb

registrations_controller.rb 已编辑

中的此方法中

 def after_sign_up_path_for(resource)
   user_profile_path(current_user.id)
 end

views/profile/show.html.erb页面上,我希望用户能够查看其个人资料并进行更新。

但是在注册后,rails会给我这个错误undefined method 'contact_person' for nil:NilClass

我不确定为什么会这样。

在我的views/profile/show.html.erb我有这个代码来访问contact_person和buisness_type。

<div class="row">

 <div class="container">
  <h3> Profile show page </h3>

  <p>Contact Person: <%= @profile.contact_person %></p>
  <p>Buisness Type: <%= @profile.profile.buisness_type %></p>

</div>

在我的users.rb和profile.rb中我有很多关系

profile.rb

class Profile < ActiveRecord::Base
  belongs_to :user
end

user.rb

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

        has_one :profile
        accepts_nested_attributes_for :profile
  end

在我的profile_controller.rb 已编辑

class ProfileController < ApplicationController
before_action :authenticate_user!

#before_action :only_current_user

def new
    #form where a user can fill put their pwn profile.

    @user = User.find(params[:user_id]) # Finnur hver er userinn sem er loggaður inn, reaching in to the url and grabbs user.
    @profile = Profile.new

end

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

def create
    @user = User.find(params[:user_id])
    @profile = @user.build_profile(profile_params)

    #Lecture 159 User show action next :))))
    if @profile.save
        flash[:success] = "Profile updated"
        redirect_to user_profile_path(current_user.id) #fer á user/show.html.erb
    else
        render action: :new

    end

end

def edit
    @user = User.find(params[:user_id])
    @profile = @user.profile

end

def update
    @user = User.find(params[:user_id])
    @profile = @user.profile


    if @profile.update_attributes(profile_params)
        flash[:success] = "Profile Updated!"
        redirect_to user_path(params[:user_id])
    else
        render action :edit
    end     
end


private

    def profile_params
        params.require(:profile).permit(:company_name, :address, :city, :postalcode, :permit_publisher, :permit_monitor, :contact_person, :contact_email, :phone_number, :buisness_type, :board_member, :description, :permit_duration)
    end

我在这里遗漏了什么吗?任何帮助将不胜感激

编辑符合Barteks的回答

当我将routes.rb更改为

Rails.application.routes.draw do
 devise_for :users, :controllers => { registrations: 'registrations' }
  resources :users do 
  resources :profile 
 end 

 root 'pages#index'
end

我现在收到此错误:ActionController::UrlGenerationError in RegistrationsController#create

no route matches {:action=>"show", :controller=>"profile", :id=>nil, :user_id=>#<User id: 14, email: "fert@fhg.is", created_at: "2016-09-10 17:46:35", updated_at: "2016-09-10 17:46:35">} missing required keys: [:id]

在registrations_controller.rb

中的这一行
def after_sign_up_path_for(resource)
 user_profile_path(current_user) 
end

我现在更加迷失了:)

1 个答案:

答案 0 :(得分:1)

您在show操作中的控制器中出现了问题,因为您的变量@profile存储了nil,但我首先建议您更改路线:

 Rails.application.routes.draw do
  devise_for :users, :controllers => { registrations: 'registrations' }
  resources :users do 
    resources :profile 
  end 
  root 'pages#index'

end

修改

当您重定向到自己的个人资料时,您应该在您的网址profile_iduser_id中加入以下内容:

redirect_to user_profile_path(user_id,profile_id)

在实践中看起来像 /用户/ something_id /简档/ PROFILE_ID

但您使用has_one关联,请尝试以下方法:

redirect_to user_profile_path(user.id,user.profile.id)