1

I'm new to web programming. So I've been searching on the web for awhile to solve this on my own, but there seems to be no one who has similar problems.

In my program, I am getting user input that contains the admins for a new booth that's being created through a textarea in my app/views/booths/new.html.erb file, as shown below.

app/views/booths/new.html.erb :

<% provide(:title, 'Create New Booth') %>
<h1>Create New Booth</h1>

<% javascript_include_tag "booths" %>

<div>
  <div>
    <%= form_for(@booth) do |f| %>
      <%= f.label :booth_name, "Name of the new Booth" %>
      <%= f.text_field :booth_name %>

      <%= f.label :booth_description, "Describe your new Booth" %>
      <%= f.text_field :booth_description %>

      <%= f.label :important_notice, "Any important notices?" %>
      <%= f.text_field :important_notice %>

      <span><br>Admins for this Booth?<br> </span>
      <textarea id="create_booth_admins_textarea" rows="30" cols="50"> </textarea>

      <%= f.submit "Create a new booth", :id => "booth_create_submit"%>
    <% end %>
  </div>
</div>

app/assets/javascripts/booths.js excerpt:

admins = $("textarea#create_booth_admins_textarea").val();

$('#booth_create_submit').click(function() {
    $.ajax({
        url: "/booths_create",
        data: {  "booth_admin_emails" : admins},
        type: "post",
        cache: false,
        success: function () {
            console.log("ajax successful");
            alert("ajax successful");
        },
        error: fucntion() {
            console.log("ajax error");
            alert("ajax error")
        }
    }); 
});

app/controller/booths_controller.rb :

def new
        @booth = Booth.new
        @other_members = []
        @booth_admins = []
    end

    def create
        temp_hash = booth_params
        @booth = Booth.new(temp_hash.except(:booth_admin_emails))
        @booth.admin_id = current_user.id               #fill in the Booth table

        @booth_admins = temp_hash[:booth_admin_emails]

        booth_admins = []

        if @booth.save
            #fill in join table
            BoothUser.create(:user_id => current_user.id, :booth_id => @booth.id, :is_admin => true) #add current user

            if !@booth_admins.nil?
                booth_admins = @booth_admins.split(",")
            end

            if !booth_admins.empty?
                booth_admins.each do |email|
                    BoothUser.create(:user_id => User.where("email=?", email).id, :booth_id => @booth.id, :is_admin => true)
                end
            end

            # just a flash to tell user that new booth was created
            name = @booth.booth_name
            flash[:success] = "New Booth #{name} created!"
            redirect_to '/booths'
        else
          render('show')
        end
    end

    private
    def booth_params
        params.require(:booth).permit(:booth_name, :booth_description, :important_notice, :booth_admin_emails)
    end

User and Booth are of has_many through association, where the join table is called BoothUser, which contains information of the booth_id, user_id, is_admin, where the id are the indicies of the specific user and the booth in their respective tables and is_admin is a boolean value checking whether the user is an admin (has the permission to edit booth settings) of the booth.

The problem arises because in my Booth table, I only declare the creator of the booth, and do not track the given admins for that booth, but am trying to find these admins for that booth by looking up the join table where the booth_id matches the index of that booth and is_admin is true.

I have been trying to pass what's being input on the create_booth_admins_textarea to :booth_admin_emails in the controller but so far had no luck, as nothing was passed onto the :booth_admin_emails. I'm guessing it's because :booth_admin_emails is not an attribute of Booth or User or BoothUser model.

And what I found on web are ways to pass arguments using strong parameters(?) to permit an attribute of a model using form_for or ajax. But no one seems to have passed input that is not an attribute of a model to the controller.

So, I wanted to ask is there a way to do so and if there is, how do I do it? Or is it just not allowed?

1 Answer 1

1

You should be able to create it like this:

BoothUser.create(user: current_user, booth: @booth, is_admin: true) #add current user
booth_admins.each do |email|
  admin = User.find_by(email: email)
  BoothUser.create(user: admin, booth: @booth, is_admin: true)
end

In your HTML, you need to create your textarea like this:

<textarea id="create_booth_admins_textarea" name="booth[booth_admin_emails]" rows="30" cols="50"> </textarea>

Then you don't need the JavaScript at all.

...however if you want the entire form to submit via AJAX, then in addition to the HTML changes above, in your JavaScript do this:

$('#booth_create_submit').click(function(e) {
    e.preventDefault(); # keeps the HTML form from submitting.
    var the_form = $(this.form);
    $.ajax({
        url: the_form.attr('action'),
        data: the_form.serialize(),
        type: "post",
        cache: false,
        success: function () {
            console.log("ajax successful");
            alert("ajax successful");
        },
        error: fucntion() {
            console.log("ajax error");
            alert("ajax error")
        }
    }); 
});
Sign up to request clarification or add additional context in comments.

6 Comments

Oh sorry... I think I have not made it clear that nothing's being passed on to the controller, so when I try adding new entries to the BoothUser table using the email I got, the :booth_admin_emails field is empty so nothing gets copied onto @booth_admins and booth_admins.... Do you happen to know why nothing would get passed on to this field?
I see. The issue seems to be that your HTML and controller are not in agreement. I'll update my answer.
You are missing a name attribute in your textarea so it doesn't get submitted along with your form.
Thank you so much! It works now! But by any chance could you explain how giving the name in that format allows for input to be passed onto the booth_admin_emails field? Again, I'm beginning to learn web programming so I have very low understanding of the language so far ;(
First of all, the textarea, just as any other input, needs a name. The is the "key" in the "key-value pairs" that are submitted. Without out a name, it's not going to be submitted. Second, Rails nests params into a hash-like array of key-value pairs. So, in your controller you would see something like params[:user][:first_name] and params[:user][:last_name]. In the HTML, to submit that structure, we need to name the inputs appropriately: user[first_name] and user[last_name]. Much of that structure is already built by form_for. Adding additional custom fields requires knowing this.
|

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.