0

I have the following code (working) to create playlists:

Controller:

def create
@playlist = current_user.playlists.build(playlist_params)
if @playlist.save

  redirect_to root_url
else
  redirect_to root_url
end
end


def destroy
end

private

def playlist_params
  params.require(:playlist).permit(:content, :name)
end

View:

<%= form_for(@user.playlists.create, remote:true) do |f| %>

  <%= f.text_field :content, id: "playlistContent", :validate => true  %>
  <%= f.text_field :name, id: "playlistName", placeholder: "Enter Playlist Name", :validate => true %>

  <%= f.submit "Save Playlist", id: "savePlaylist", remote:true %>
<% end %>

Now I wish to allow the user to add a new song to playlist content. I'm using a typeahead where a user enters the Playlist name, and upon selecting the name it will fill in a hidden ID field with the playlist ID (in case of multiple playlists with the same name). If necessary, I will include the full typeahead code here. I am attempting to update the playlist along the following lines (based on this earlier question: Problems keeping an object in an array, Ruby issues and Rails issues )

My main issue is trying to pass through the ID of the playlist the user selects to the rails form, (rails being server side and not having ability of client-side methods like jQuery to get the text field value as I understand). The update_attribute and update_column methods seemd to be limited to passing through (name, value), and I can't figure out how to pass through the ID so I can push through the new song to the existing playlist content array.

Community
  • 1
  • 1
Amir
  • 281
  • 5
  • 15
  • One thing to watch out for: `create` actually creates a record. You probably don't want that happening in your `form_for`. – Michael Westbom May 13 '14 at 00:03
  • Yeah, for my `def update` in the controller I have `@playlist = Playlist.find(params[:id])`, but I am selecting the `:id` client-side with typeahead + JSON, and where I'm having difficulty passing the ID into the `form-for`. I've tried having the hidden field precede the `form_for` helper so I can pass the ID in as an argument, but get various errors (such as 0 arguments for 2, or cannot find ID=id). – Amir May 13 '14 at 08:06

1 Answers1

1

Pass the ID you are collecting on the client side into an

f.hidden_field 

inside your form. It will then be submitted to the controller along with the other parameters.

You may be using update_attribute and update_column incorrectly if you expecting them to get the ID. Once you are in the controller, update_attribute is used to update data into the model, not to retrieve data from the form.

Edit: As discussed in the chat, one way to do what you are trying to achieve (i.e. create and update from a single form) is to have a hidden field that you populate with the playlist ID following your AJAX call to check if the playlist already exists.

When it gets to your controller, you can use this ID as the basis for determining if you create a new playlist or update an existing model.

One difficulty in using the same page to search for a playlist and then create or update is that in the case of updating an existing playlist you may need to make the appropriate calls to update the form with the pre-existing song list and playlist details.

Dave Satch
  • 1,161
  • 6
  • 12
  • Yeah, I should have been more clear, I already have a hidden field for ID, my problem is that I can't figure out how to pass the value from that hidden field into the controller to update a playlist with a certain ID. I'm using typeahead + JSON to create a .json page with the appropriate values for the typeahead input, but because that is client-side, I don't know how to pass that into a rails form where I want to update a column for a playlist with a specific ID. – Amir May 13 '14 at 08:02
  • You will need to do some form of post back. If you are setting it on the form as a hidden field then it will be available to the controller once the user submits the form. i.e. params['my_hidden_field']. If you are not already submitting that form, then you will most likely need some sort of AJAX post back, which is a different way of imagining your page. – Dave Satch May 13 '14 at 08:07
  • If I try and pass something like `form_for(@playlist[params[:hiddenField]])`, I get `undefined method `[]' for nil:NilClass`. I did think about an AJAX post back, but I guess it will have to require an overhaul about how the page would work, like you said. – Amir May 13 '14 at 08:15
  • If you can get to the chatroom we can discuss - that's not where you want the param to be - http://chat.stackoverflow.com/rooms/5676/ruby-on-rails – Dave Satch May 13 '14 at 08:32