"Undefined method 'delete' for nil:NilClass" in Sinatra - ruby-on-rails

I'm working on a project in Sinatra and I can't seem to get the delete method to work. My intent is to be able to remove an object using a form in a modal. Here's what I have:
routes.rb:
delete '/songs/:id/delete' do
#song = Song.where(:id => params[:id]).first
#song.delete
redirect to '/songs'
end
index.erb:
<form action="/songs/:id/delete" method="post">
<input type="hidden" name="_method" value="delete">
<div id="song_id">
<label>id:</label>
<input type="text" name="id">
</div>
<button type="submit" id="delete">Delete</button>
<div id="back">Back to Songs</div>
</form>
Feedback is appreciated.
(Also, sorry indentation isn't perfect)

You have to inject the id into the form. Not :id.
# example
<form action="/songs/1234/delete" method="post">
Also, you can see what is happening with puts params.

Related

Redirect after uploading to S3 from rails app

I have a simple file upload field:"
<form action="<%= #post.url %>" method="post" enctype="multipart/form-data" id="new_file_tag">
<% #post.fields.each do |name, value| %>
<input type="hidden" name="<%= name %>" value="<%= value %>"/>
<% end %>
<input type="file" name="file"/>
<input type="submit" class="btn btn-awaken btn-sm">
</form>
that I took from the S3 online docs. The only issue is that it redirects to the S3 object after upload rather than simply returning to the same page.
How can I change the form to accomplish this? I tried changing the form to rails helpers, but the upload would fail due to the automatic accept-charset="UTF-8" param.
The form should contain a field for the success_action_redirect option, which has the value of the URL you would like to redirect the user to following a successful post.
https://docs.aws.amazon.com/sdkforruby/api/Aws/S3/PresignedPost.html#success_action_redirect-instance_method

routing form submission to different controller

problem: I have a form but the submit button doesn't do anything.
i instantiate the class the form is for in the employee_controller controller.
def employee
#body_class = "employee membership"
#employee_contact_form = CorporateContactForm.new
end
I create the form in the page the above controller action serves
= simple_form_for [#employee_contact_form] do |f|
= f.input :firstname
= f.button :submit
In my routes I set the resources for the contact forms
resources :corporate_contact_forms
I then have a controller that serves the form
class CorporateContactFormsController < ApplicationController
def create
raise
end
I am aware there is no code in the corporatecontactcontroller, but the submit button should at least fire to an error. It doesn't do anything.
This feels like such a simple problem, and surely it is.
What am I missing?
update
html output
<form>
<form accept-charset="UTF-8" action="/corporate_contact_forms" class="simple_form new_corporate_contact_form" id="new_corporate_contact_form" method="post"><div style="display:none"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="7iMwWQOuyzV3jJt4jTtr9MGvI129hPaG+m+Pe2D3YyM=" /></div> <div class="input string optional corporate_contact_form_firstname"><label class="string optional" for="corporate_contact_form_firstname">Firstname</label><input class="string optional" id="corporate_contact_form_firstname" maxlength="255" name="corporate_contact_form[firstname]" size="255" type="text" /></div>
<input class="button" name="commit" type="submit" value="Create Corporate contact form" />
</form>
</form>
According to the documetation (http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for), you can do something like this to force the form to go to CorporateContactFormsController#create
<%= simple_form_for #employee_contact_form, url: corporate_contact_forms_path, method: :post do |f| %>
# awesome code...
<% end %>
Also I'm not sure if the f.button :submit has something to do, the default for submitting is f.submit

How to post html form data to controller using ruby on rails 3.1

I am using ruby on rails 3.1. And am trying to post html form data to controller for saving the record in database. But I am getting routing error like this 'No route matches [POST] first/save' .But when I tried to run this link in address bar like '127.0.0.1:3000/first/save' it is working fine. Can any one please tell me where am doing wrong.
My routes are like:
Rails.application.routes.draw do
root 'first#hello'
get 'first/save'
end
And my html form is like:
<form accept-charset="UTF-8" method='post' action='/first/save'>
<label for='S.No'>S.No</label>
<input type="text" name="s_no" placeholder='Enter s. no.'>
<label for='name'>Name</label>
<input type="text" name='name' placeholder='Enter your name'> <br>
<label for='seller_id'>Seller ID</label>
<input type="text" name='seller_id' placeholder='Enter your seller ID'> <br>
<label for='email'>Email</label>
<input type="email" name='email' placeholder='Enter your email'> <br>
<label for='img_url'>Image</label>
<input type='text' name='img_url' placeholder='Enter your image url'> <br>
<input type="submit" name='save' value='Save'>
</form>
And here is my controller:
class FirstController < ApplicationController
def save
#name = params[:name]
#email = params[:email]
#seller_id = params[:seller_id]
#img_url = params[:img_url]
#s_no = params[:s_no]
end
end
If you want to do POST requests, instead of
get 'first/save'
you should have
post 'first/save'

Create a search page in ActiveAdmin

I'm using ActiveAdmin to deploy my project. And I had some problem when I was developing.
I had some database table, e.g: "Worker", "Product", "Task". I wanted to create a page to search on those table with many situation.
I created a simple page:
ActiveAdmin.register_page "my page" do
content do
panel "Search details" do
panel "By Name" do
render :partial => "form"
end
end
end
end
And this is _form.html.erb
<form action="" method="post">
<input type="text" value="" name="taskid">Task ID</input>
<input type="text" value="" name="productid">Product ID</input>
<input type="text" value="" name="workerid">Worker ID</input>
<input type="submit" value="Submit"/>
</form>
But I don't know how can I call a controller from a form? (which controller was defined)
And How can I render or show the result to the content area of "my_page" in Activeadmin from a controller?
Anyone may help me? plz!
Design the form such that it uses the existing active admin filters and displays the results accordingly. You may want to look at the HTML of your filter forms (using firebug), in order to get the required params, and the submit action. Here is the partial which I use to search my User model (constructed from user filters):
<div class="panel_contents">
<form method="get" id="q_search" class="filter_form" action="/admin/users" accept-charset="UTF-8">
<div class="filter_form_field filter_string">
<label for="q_email" class=" label">Search User Email</label><br/>
<input type="text" name="q[email_contains]" id="q_email" />
<input type="submit" value="Go" name="commit" id="q_submit" />
</div>
</form>
</div>
and displays the result in the existing format. You don't need to design new views for that.

How to create a delete form with RESTful routes in Rails.

I am trying to create a form that serves as confirmation for the destroy method on a controller. In my routes I have:
resources :leagues do
get 'delete', :on => :member
end
In my delete.html.erb, I have the following for:
<% form_for current_league, :html => {:method => :delete} do |form| %>
<%= form.submit 'Yes' %>
<%= form.submit 'No', :name => 'cancel' %>
<% end %>
current_league is a helper function defined by:
def current_league
#current_league ||= League.find_by_id(params[:id])
end
So the problem is that the form that is generated only edits the league model, as seen by the form method="post".
<form accept-charset="UTF-8" action="/leagues/1" class="edit_league" id="edit_league_1" method="post">
<div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" />
<input name="_method" type="hidden" value="delete" />
<input name="authenticity_token" type="hidden" value="abcdefg" />
</div>
<input id="league_submit" name="commit" type="submit" value="Yes" />
<input id="league_submit" name="cancel" type="submit" value="No" />
</form>
How can I fix this?
I think that will already work. As in rails guide http://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-patch-put-or-delete-methods-work-questionmark
However, most browsers don’t support methods other than “GET” and “POST” when it comes to submitting forms.
Rails works`around this issue by emulating other methods over POST with a hidden input named "_method", which is set to reflect the desired method:
as you see in the output form there is
<form accept-charset="UTF-8" action="/leagues/1" class="edit_league" id="edit_league_1" method="post">
....
<input name="_method" type="hidden" value="delete" />
....
</form>
When reading this variable, rails understood that this is a DELETE method, not a POST method. even though the form it self is a POST.

Resources