Rails:尝试创建用户配置文件时出错

Rails: error while trying to create user profiles

我是 rails 的新手,我正在尝试学习它 我已经使用控制器生成了配置文件模型:

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

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

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_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 ?

我相信这条线

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

应该是

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

<%= 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 %>

你的 UsersController 中间多了一个 'end'。应该是:

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

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

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