【问题标题】:Rails: error while trying to create user profilesRails:尝试创建用户配置文件时出错
【发布时间】:2015-06-21 16:07:57
【问题描述】:

我是 Rails 新手,我正在努力学习它我已经使用控制器生成了一个配置文件模型:

rails g model Profile twitter:string, about:text, country:string

现在我正在尝试将用户(使用 devise 创建的)与配置文件相关联,我已经通过这种方式完成了配置文件和用户关联:

用户.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_many :posts
 has_many :comments 
 has_one :profile, dependent: :destroy

    has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100#" }, default_url: "/images/:style/missing.png"
      validates_attachment_content_type :avatar, content_type:  /\Aimage\/.*\Z/        
    
    end

profile.rb

class Profile < ActiveRecord::Base
	belongs_to :user
end

ProfilesController.rb

class ProfilesController < ApplicationController

def new
	@profile = Profile.new
end

def create
	@profile = Profile.new(params[:profile].permit(:twitter, :about, :country))

if @profile.save
	redirect_to profile_path
else 
	render 'profile'
end

end

end

UsersController.rb

class UsersController < ApplicationController
	def create
	end 
	def index
		
	end
end

我正在尝试渲染的表单部分

_profile.html.erb

<%= form_for ([@user, @profile.user_build])  @profile do |f| %>
<%= current_user.name %>
<%= f.text_area:about, label: 'Name' %>
<%= f.text_field :twitter, label: 'Your Twitter' %>
<%= f.text_field :country, label: 'Your Country' %>
<% end %>

现在当我转到 localhost:3000/profiles/new 时出现此错误

    /home/alexandrov/web_development/thedesignable/app/views/partials/_profile.html.erb:1: syntax error, unexpected tIVAR, expecting keyword_end ...@profile.user_build]) @profile do |f| @output_buffer.safe_a... ... ^ /home/alexandrov/web_development/thedesignable/app/views/partials/_profile.html.erb:7: syntax error, unexpected keyword_ensure, expecting end-of-input
<%= form_for ([@user, @profile.user_build])  @profile do |f| %>
<%= current_user.name %>
<%= f.text_area:about, label: 'Name' %>
<%= f.text_field :twitter, label: 'Your Twitter' %>
<%= f.text_field :country, label: 'Your Country' %>
<% end %>

我的 routes.rb 文件看起来像这样

    Rails.application.routes.draw do
  devise_for :users
  mount Ckeditor::Engine => '/ckeditor'
  get 'pages/home'

  get 'pages/thecompany'

  get 'pages/ourwork'

  get 'pages/plansandpricing'

  get 'pages/tour'

  get 'pages/tutorialsandvideos'

  get 'pages/contact'

  get 'pages/faq'

  get 'pages/tandc'

 resources :posts do
    member do
      get "like", to: "posts#upvote"
      get "dislike", to: "posts#downvote"
    end
    resources :comments
  end
  resources :users
  resources :profiles

  root 'pages#home'
end

我不知道我做错了什么,我需要什么类型的迁移..我需要做 rails g migration AddUserIdToProfile 吗?

【问题讨论】:

    标签: ruby-on-rails devise profile one-to-one user-profile


    【解决方案1】:

    我相信这条线

    <%= form_for ([@user, @profile.user_build])  @profile do |f| %>
    

    应该是

    <%= form_for ([@user, @profile.user_build]) do |f| %>
    

    【讨论】:

    • 我正在尝试获取为用户生成的用户配置文件(现有的和新的)。我已经编辑了那行,现在得到这个错误: undefined method `profile_build' for nil:NilClass
    • @Victor.A 哪个文件包含该部分?
    • partials/_profile.html.erb 正在渲染到编辑和更新视图中
    【解决方案2】:

    <%= form_for ([@user, @profile.user_build]) @profile do |f| %> <%= current_user.name %> <%= f.text_area:about, label: 'Name' %> <%= f.text_field :twitter, label: 'Your Twitter' %> <%= f.text_field :country, label: 'Your Country' %> <% end %>

    【讨论】:

      【解决方案3】:

      我终于用另一种方式解决了这个问题,我使用设计数据字段和用户“显示”视图来实现这一点。我现在可以让用户注册并使用相同的数据在显示视图中获取。

      一切正常。如果有人需要了解更多信息,请告诉我。我很乐意帮助你。

      【讨论】:

        【解决方案4】:

        您的 UsersController 在中间有一个额外的“结束”。应该是:

        class UsersController < ApplicationController
          def create; end 
          def index;end
        end
        

        【讨论】:

        • 对不起,我把错字更正了。仍然没有帮助
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-24
        • 2022-06-19
        • 2016-03-15
        • 1970-01-01
        • 2014-04-21
        • 1970-01-01
        相关资源
        最近更新 更多