0

I have the following controller actions:

def new
   @tag = Tag.new({company_id: current_member.company_id })
  end

 def create
    @tag = Tag.new(tag_params)
    @companyid = current_member.company_id
    respond_to do |format|
      if @tag.save
          format.html {redirect_to root_path}
          format.json {render :json=> @tag}
      else
        format.html {redirect_to root_path}
      end
    end
  end

Inside my new.html.slim view file I have:

= simple_form_for @tag, url: tags_new_path, html: {id:'tag_form_submit', method: :post}, format: :js, remote:true do |t|
  =t.input :name, placeholder: 'tag'
  =t.button :submit, "submit tag", id:"submit_tag_button"

javascript:
  $(document).ready(function(){

    $("#submit_tag_button").click(function(event){
      event.preventDefault();
      <!-- $("#tag_form_submit").hide(); -->
      <!-- $('#add_tag_tag').hide(); -->
      var text = $("#inputbox").val();
      var name = $("#tag_name").val();
      var datatosend = { tag: {name:name, company_id: "#{@companyid}" } };
      $.ajax({
        url: "/tags/new",
        dataType: "json",
        method: "post",
        data: datatosend,
        success: function(result){
          console.log(result)
          var nameVar=result.name;
          var idVar=result.id;
          console.log(nameVar);
          console.log(idVar);
          var newspan='<span class="checkbox"><label for="job_tag_ids_'+idVar+'" name="job[tag_ids]"><input class="check_boxes optional form-control" data-remote="true" id="job_tag_ids_'+idVar+'" name="job[tag_ids][]" type="checkbox" value="'+idVar+'">' + nameVar + '</label></span>';
          $("#all_tags > div.form-group.check_boxes.optional.job_tags > div").append(newspan);
          <!-- $(#event_tag_ids).append('hi'); -->
          if ($("#event_tag_ids").length){
              $("#event_tag_ids").append('<option value="'+idVar+'">'+nameVar+'</option>');
          }

          // after its appended, erase from form input
           console.log(result.company_id);
          $("#tag_name").val("");
        }
      })
    })
  });

In my datatosend varible I am trying to send:

   var datatosend = { tag: {name:name, company_id: "#{@companyid}" } };

but @companyid is being posted as null. Why?

How do I successfully access @companyid from the controller inside my JavaScript so I can send it in when making my AJAX post call?

1 Answer 1

1

You're only assigning the @companyid instance variable in the create action of your controller, not the new action.

The view template you posted is rendered as part of the new action. So you should change your controller:

def new
  @companyid = current_member.company_id
  @tag = Tag.new(company_id: @companyid)
end
Sign up to request clarification or add additional context in comments.

2 Comments

that was it!! such a silly mistake - sometimes its helpful to use a pair of second eyes , id been struggling with this issue for a long time! thanks again!
...or since the company_id is an attribute of the tag, @tag.company_id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.