Rails: error while trying to create user profiles

82 views Asked by At

I am new to rails and i am trying to learn it I have generated a profile model with controller using :

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

Now i am trying to associate the User(that is created with devise) with profiles, I have done the profile and user association this way:

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

The form partial i am trying to render

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

Now i am getting this error when i go to localhost:3000/profiles/new

    /home/alexandrov/web_development/thedesignable/app/views/partials/_profile.html.erb:1: syntax error, unexpected tIVAR, expecting keyword_end [email protected]_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 %>

My routes.rb file looks like this

    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

I can't get to know what i am doing wrong, also what type of migration do i need.. do i need to do rails g migration AddUserIdToProfile ?

4

There are 4 answers

0
Victor.A On BEST ANSWER

I've finally fixed this the other way, I used devise data fields and users "show" view to achieve this. I am now able to get user to sign up and use same data to be fetched in show view.

Everything is working perfectly fine. In case anyone need to know more let me know. I would be glad to help you out.

1
Thupten N Chakrishar On

You UsersController has an extra 'end' in the middle. Should be:

class UsersController < ApplicationController
  def create; end 
  def index;end
end
3
Pavan On

I believe this line

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

should be

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

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