Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Status Author Name Error

Devise Statuses EmbeddedRuby DB

  • Please log in to reply
4 replies to this topic

#1 vikhyat620

vikhyat620

    Passenger

  • Members
  • 9 posts

Posted 06 May 2014 - 05:18 AM

I am making a social app, and have made a user authentication system in Devise. I customized the forms by adding FIrst, Last and user names to the forms.

 

I also made a statuses model as in Facebook Status, so regsitered users can post a status,

$ rails g scaffold status name:string content:text

I used a devise helper and made the page content only available to signed in users,but im having a problem defining the status author name as :

current_user.full_name

in My user.rb i added:

def full_name 


first_name + " " + last_name
end
how do i make the value of Name in the statuses page, the current user's full name, this value cant be changed by the user as well.
If any other files are required ill upload them


#2 Jamie

Jamie

    Controller

  • Moderators
  • 140 posts
  • LocationNewcastle, UK

Posted 07 May 2014 - 03:31 PM   Best Answer

Hi

 

Welcome to Rails Forum!

 

When you say you've customised Devise and added the first and last names to the forms, would you mind sharing the schema.rb or migration for that file so I can see how it's setup.

 

As for your code, it should work as it is if they have a first and last name specified. I'd possibly rewrite it to the following:

def full_name
  "#{first_name} #{last_name}"
end

But if your ApplicationController.rb I would add the following as a protected method:

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) << [:first_name, :last_name]
end

but don't forget to add the following code as before_action:

before_action :configure_permitted_parameters, if: :devise_controller?

And add the necessary form fields to the view. If you share what you have for the view I'll let you know what you need to add/change.

 

Hope this helps!

 

JB


Rails developer based in Newcastle, UK.


#3 vikhyat620

vikhyat620

    Passenger

  • Members
  • 9 posts

Posted 07 May 2014 - 03:54 PM

Thank you! Your explanationwas crystal clear, and i also went ahead a made a hidden field in the div for the name and defined it as the current_user.full_name in the post-controller.rb. Thanks again, i was bumming out about this.



#4 Jamie

Jamie

    Controller

  • Moderators
  • 140 posts
  • LocationNewcastle, UK

Posted 07 May 2014 - 04:22 PM

Hi

 

Glad I could help. But what you're describing about having hidden fields seems unnecessary if you're using an authentication system.

 

If you've setup relationships correctly in your model, you should be able to do something like current_user.posts.new(post_params)

 

Share the controller for that page and I'll point you in the right direction.

 

All the best.

 

 

JB


Rails developer based in Newcastle, UK.


#5 vikhyat620

vikhyat620

    Passenger

  • Members
  • 9 posts

Posted 08 May 2014 - 04:04 AM

My Posts Controller:

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]
 
  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
  end
 
  # GET /posts/1
  # GET /posts/1.json
  def show
  end
 
  # GET /posts/new
  def new
    @post = Post.new
  end
 
  # GET /posts/1/edit
  def edit
  end
 
  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(post_params)
    @post.name = current_user.first_name + " " +current_user.last_name
    @post.username = current_user.user_name
 
    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render action: 'show', status: :created, location: @post }
      else
        format.html { render action: 'new' }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end
 
  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end
 
  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end
 
  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])
    end
 
    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:name, :content)
    end
end
 
 
Views: _form
<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
 
      <ul>
      <% @post.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
 
  <div class="field">
    <%= f.hidden_field :username %>
    <%= f.hidden_field :name %>
  </div>
  <div class="field">
    <%= f.label :content %><br>
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
 






Also tagged with one or more of these keywords: Devise, Statuses, EmbeddedRuby, DB

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users