0

I'm a Rails newbie, and I have a RoR app that I'm working on, the app is supposed to allow invited guests to RSVP to a wedding.

I have allowed for fields to be dynamically added, in order to include additional guests (family of an invited guest). But when I add the dynamic field and add the names, only the last name is displayed in the index.

Is there anything specific I need to do to render all other names together in a field within the table?

This is my current controller thus far:

class GuestsController < ApplicationController
skip_before_filter :authenticate_user!, only: [:new, :create]

  def index
    @guests = Guest.all
    
  end

  def new
    @guest = Guest.new
  end

  def create
    @guest = Guest.all
    @guest = Guest.create(guest_params)
    if @guest.save
      respond_to do |format|
        format.html { redirect_to :back, notice: 'Thank you for replying' }
        format.js
      end
    else
      respond_to do |format|
        format.html { render 'new' }
        format.js
      end
    end
  end

  def destroy
    @guest = Guest.find(params[:id])
    @guest.destroy
    redirect_to guests_path
  end

private
  
  def guest_params
    params.require(:guest).permit(:status, :name, :message)
  end

end

4
  • 3
    I would suggest you to pick up a good book on Rails to start with(like Agile Web Development with Rails) and come back with specific questions when you get stuck with something. Also the paperclip documentation has a good overview on how to start with its integration in a Rails app. Commented Mar 9, 2015 at 13:49
  • I agree with @AlokSwain that you need to find a good book. This question is rather broad. But broadly speaking, you need to figure out what kind of data you require first. In Rails, you can add or update tables for your db using the Migration capability (Google search, "Rails migrations"). Once you've defined what your data looks like and what you need to "see", then you can start adding models/views/controllers as needed. Commented Mar 9, 2015 at 14:00
  • I agree with @AlokSwain as well ... and The Rails 4 Way is another good one to use as is Practicing Rails. Both books greatly help new Railsy folks to get going. In regards to your current issue, I, personally, would want to know what the model structure is like for RSVPing to a wedding. For example, do you have an Rsvp model that has_many Guests (if something like this were the case, there are gems and tools for adding nested objects to the Rsvp form)? The more specific and detailed your question and supporting info is, the better responses you'll get! Commented Mar 10, 2015 at 13:59
  • I have a User model that has_many guests, and Guest belongs_to User. I saw that there is a cocoon gem, however it seems that I will have to create nested attributes. the app works well when you enter only one :name, as soon as you add another :name field, then you can only see the last one entered Commented Mar 10, 2015 at 14:12

1 Answer 1

1

Place Guest in place of User

users controler

  def new
   @users = []
   5.times do
   @users << User.new
  end
 end

 if params.has_key?("user")  
    User.create(users_params(params["user"]))
  else
    params["users"].each do |user|
     if user["name"] != nil || user["age"] != nil
      User.create(users_params(user))
     end
    end
  end

 def users_params(my_params)
   my_params.permit(:name, :age)
 end

users_form

<%= form_tag users_path do  %>


<% @users.each do |user| %>
<%= fields_for 'users[]', user do |u| %>




<div class="field">
<%= u.label :name %><br>
<%= u.text_field :name %>
</div>
<div class="field">
<%= u.label :age %><br>
<%= u.number_field :age %>
</div>
<% end %>
<% end %>
<div class="actions">
<%= submit_tag %>
</div>

<% end %>
Sign up to request clarification or add additional context in comments.

Comments

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.