I am having problems with the following scenario:
My users do searches by keywords which produces a list. The user has 2 actions either add them to a favorites table or block them using check boxes.
The problem I have is that when users click "Add to Favorites" the form passes a list of hashes to my strong params method and I am not able to pass it correctly.
I think the problem is that the hash required by strong_params is inside another hash.
Also I have no idea on how to pass the same hash to the BlockController when user click "Block"
This is the error message:
param is missing or the value is empty: {:favorites=>{:name=>"Jon Doe", :title=>"Provider", :company=>"Acme", :location=>"Dubai", :profile=>"Group A", :notes=>""}}
My results.html.erb is
<table class="table table-striped table-bordered">
<tr>
<th class="center">Name</th>
<th class="center">Title</th>
<th class="center">Company</th>
<th class="center">Location</th>
<th class="center">Profile</th>
<th class="center">Select</th>
</tr>
<%= form_tag favorites_path do %>
<%= @results.length %>
<% @results.each do |key,value| %>
<tr>
<td><%= value['name'] %></td>
<td><%= value['title'] %></td>
<td><%= value['company'] %></td>
<td><%= value['location'] %></td>
<td><%= link_to 'Profile', value['profile'],:target => "_blank"%></td>
<td><%=check_box_tag 'favorite[]', {:name => value['name'],:title =>value['title'],:company => value['company'],:location => value['location'], :profile=> value['profile'], :notes =>""}%></td>
</tr>
<% end %>
<%= submit_tag "Add to Favorites", name: 'add', class: 'btn btn-primary' %>
<%= submit_tag "Block Profiles", name: 'block', class: 'btn btn-danger' %>
<% end %>
</table>
this is how my strong_params method looks:
def favorite_params
params[:profiles].each do |e|
params.require(e).permit(:name, :title, :company, :location, :profile, :notes)
end
end
Any ideas?
Update:
I am able to pass params as:
def favorite_params
params.permit(:commit,favorite:[])
end
create method:
def create
@favorite = Favorite.new(favorite_params)
@favorite.user_id = current_user.id
respond_to do |format|
if @favorite.save
format.html { redirect_to @favorite, notice: 'Favorite was successfully created.' }
format.json { render :show, status: :created, location: @favorite }
format.js { render :show, status: :created, location: @favorite }
else
format.html { render :new }
format.json { render json: @favorite.errors, status: :unprocessable_entity }
end
end
end