我是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 ?
答案 0 :(得分:0)
我相信这一行
<%= form_for ([@user, @profile.user_build]) @profile do |f| %>
应该是
<%= form_for ([@user, @profile.user_build]) do |f| %>
答案 1 :(得分:0)
<%= 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 %>
答案 2 :(得分:0)
您的UsersController在中间有一个额外的'结束'。应该是:
class UsersController < ApplicationController
def create; end
def index;end
end
答案 3 :(得分:0)
我终于修复了另一种方式,我使用了设计数据字段和用户“显示”视图来实现这一点。我现在能够让用户注册并使用相同的数据在show视图中获取。
一切都很好。如果有人需要知道更多,请告诉我。我很乐意帮助你。