Felipe Valdivia
Felipe Valdivia

Reputation: 369

Rails Create article with other user not current_user

I would like to create an article with other user not current_user and for that I'm saving in a session the id to the other user and I recover this id with a collection in the view to this point everything work fine but when I'm trying to use my helper :selected_user into my articles controller with a if sentence doesn't work here is my code:

def new
    if selected_user.present?
      @article = selected_user.articles.build state: :step1
      render_wizard
    else
      @article = current_user.articles.build state: :step1
      render_wizard
    end
  end 

so, I'm asking if the selected_user.present? I would like to create the article with this user_id but else I would like to create it with the current_user

my create method is:

def create
  if selected_user.present?
    step = :step1

    @article = selected_user.articles.build article_params_step1
    @article.state = step.to_s

    if @article.save
      redirect_to wizard_path(next_step, article_id: @article)
    else
      render_wizard
    end
  else
    step = :step1

    @article = current_user.articles.build article_params_step1
    @article.state = step.to_s

    if @article.save
      redirect_to wizard_path(next_step, article_id: @article)
    else
      render_wizard
    end
  end
end

so, yeah when I run my view the controller jump to the else section.

just for clarify my selected_user not return nil but here is the implementation:

selections_controller.rb:

class SelectionsController < ApplicationController
  before_action :authenticate_user!

  def create
    session[:selected_user_id] = params[:user][ :user_id]
    redirect_to root_path
  end
end

and in my application_controller.rb:

helper_method :selected_user

def selected_user
  @selected_user ||= User.find(session[:selected_user_id])
end

and in the view:

<%= form_tag( { :controller => "selections", :action => "create" } , :class => "navbar-form navbar-left") do %>
 <%= collection_select(:user, :user_id, User.all, :id, :name, prompt: "Escoge cliente")%>
 <%= submit_tag 'Enviar'  %>
<% end %>

if I try create an article without select an user from my collection appear this error:

Couldn't find User with 'id'=

but when I select my user from the collection everything works fine. so just I want when I don't select nothing create with the current_user.

Thanks for your time !

Regards !

Upvotes: 0

Views: 380

Answers (2)

Dharam Gollapudi
Dharam Gollapudi

Reputation: 6438

The reason why you were seeing the error

Couldn't find User with 'id'=

when you haven't selected a user was that the session[:selected_user_id] was nil and your old selected_user with following code was throwing the error.

def selected_user
  @selected_user ||= User.find(session[:selected_user_id])
end

User.find method expects either a single id or an array of ids. If you give a single id and if it finds the relevant record in the database then it will returns that instance. If you give an array of ids and if it finds those relevant records in the database, then it will return array of those instances. But if you pass nil to it, then it will through the error Couldn't find User with 'id'= as it won't find a relevant record.

But your updated selected_user implementation:

def selected_user
  @selected_user ||= session[:selected_user_id] && User.find_by_id(session[:selected_user_id])
end

is working because, first you are checking for the existence of session[:selected_user_id] value and second you are using User.find_by_id instead of User.find.

User.find_by_id either returns a single instance of the record if it finds it in the database or will return nil if it doesn't find the record. It will never through an error.

Refer to ActiveRecord#find and ActiveRecord#find_by for more info.

Upvotes: 1

Felipe Valdivia
Felipe Valdivia

Reputation: 369

I'm not sure why is working and what is the different but my solution for the problem it was to add this to my selected_user method:

def selected_user
  @selected_user ||= session[:selected_user_id] && User.find_by_id(session[:selected_user_id])
end

and with that I don't have the nil error and entry to the if statement without errors.

Upvotes: 0

Related Questions