Ruby-on-Rails post/get parameters value is NULL - ruby-on-rails

I am new to Ruby on Rails so I apologize if the answer is obvious.
Here is my routes
get 'user_location/show' => 'user_location#show'
Form in one of my pages
<form action="/user_location/show" method="post">
<input type="text" name="destination" />
<input type="submit" name="commit" value="Find city" />
</form>
Now when I try to print out my post variables on my webpage
I get null. Something like this
<b> The selected destination is </b>
<%params['destination']%>
What am I doing wrong? I suspect it has something to do with routes but I am not too sure. Any help would be appreciated.
Thanks

You're not printing params in your view.
There is typo:
<%params['destination']%>
should look like
<%= params['destination']%>
Another suggestion is to use Form Helpers for generating forms.

Since you are using POST method for form submission, so your HTTP Verb for user_location/show have to include POST also. So your route would be:
match 'user_location/show' => 'user_location#show', via: [:get, :post]
And in your view:
<%= params['destination'] %>

Related

Rails: How to send hidden paramters in link_to

I wanna do this in rails way.
<form action="/home/result" method="GET">
<input name="hidden-query" value="hidden-data" type="hidden" >
<input name="public-query" value="public-data">
</form>
I tried with
<%= link_to 'TO RESULT',home_result_path(public_query: public_data,hidden_query: hidden_data)%>
It shows hidden_query parameter in view's url.
What I wanna do is hiding hidden_query.
How can I do it? Thanks!

Rails Turbolinks does not render response from submitted form

Submitting a basic xhr-form in a turbolinks-activated rails project (5.1), does trigger the correct server-side response but the client (turbolinks) discards the response. Neither get nor post do work. Tested in Firefox and Chromium. Forms with redirection work as intended.
Here's a simple test page that fails for me
test.html.erb:
<h1><%= #value %></h1>
<%= form_with url: 'test' do |f| %>
<%= f.check_box :anything %>
<%= f.submit %>
<% end %>
controller:
def test
if request.post?
#value = 'post'
else
#value = 'get'
end
end
routes:
post '/test', to: 'controller#test'
get '/test', to: 'controller#test'
rails response payload:
<h1>post</h1>
<form action="test" accept-charset="UTF-8" data-remote="true" method="post"><input name="utf8" type="hidden" value="✓" /><input type="hidden" name="authenticity_token" value="X1sh+tc/Jx4P0rdDqc2CD1RetKQKr+lVh4fD29JOMY/b+PdPNgw1qyzoyv6t3I2I+6jV1mmn6voXN+YySaUISw==" />
<input name="anything" type="hidden" value="0" /><input type="checkbox" value="1" name="anything" />
<input type="submit" name="commit" value="submit" data-disable-with="submit" />
</form>
</div>
The form loads like expected, but after submitting the page does not change. Inside the xhr-response the correct answer is visible, but it does not get applied to the DOM.
Is this a bug, or am I using turbolinks wrong?
I'm seeing this as well and I'm not sure how to proceed. What is nice about Turbolinks is that you don't have to create js responses for each request. This is true until you submit a form. At that point, you have no choice but to create a js.erb respone file or change the form to local: true. Am I missing something?
Try using data: { turbolinks: false } in the any links to the page containing the form.
E.g.
<%= link_to "Get in Touch", 'contact', data: { turbolinks: false } %>

"Undefined method 'delete' for nil:NilClass" in Sinatra

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.

Rails button_to for Custom Name / Value Submit

In Rails is it possible to achieve an HTML output like this using button_to?
<button type="submit" value="1" name="id">Type 1</button>
<button type="submit" value="2" name="id">Type 2</button>
What are the reasons for using button_to over manually entering the HTML in my form?
...
EDIT:
Perhaps I should rephrase this? Seems the better way would be to add some hidden fields to the button_to form. It doesn't seem like I can do this.
So what is the correct Rails solution to pass extra hidden fields to a dynamic form generated via a button_to? Or should I just build a form manually?
e.g. I have:
button_to "Download", items_path(:release_id => release), :remote => true
and I want to pass an extra parameter in the form POST format => "download"
Seems my answer was to use the form_tag helper

asp.net mvc posting form values using html.actionlink

how can i post the form values using html.actionlink, don't want to use routes dictionary
<%=Html.ActionLink("Download", "MyFiles", "Jobs", null, new { #class = "cvclick" })%>
A link points to an HTTP GET request.
An HTTP GET request is sent to a URL; the URL must be defined using a route.
To POST values you could use an HTML form:
<% using (Html.BeginForm("MyFiles", "Jobs")) { %>
<%= Html.Hidden("key1", "value1") %>
<%= Html.Hidden("key2", "value2") %>
<input type="submit" value="Download" />
<% } %>
To POST values you could use Spark View Engine and a HTML form:
<form action="myfiles" controller="jobs">
<hidden name="key1" value="value1" />
<hidden name="key2" value="value2" />
<submit title="Download" />
</form>
(The code uses some pretty standard bindings that be wired in Spark)
As for links and ActionLink. I would use the ajax helper instead since it can POST stuff. (Ajax.ActionLink)
Edit
So you want to DOWNLOAD a file? Well. The link should point on an action in your controller. The action should return a FileResult with your file. See here: http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.file(v=vs.90).aspx
You can post the data using :-
anthing inside Beginform will be posted
<% using (Html.BeginForm("ActionName", "ControllerName"))
{ %>
texbox code
<input type="submit" class="dASButton" value="Submit" />
<% } %>

Resources