How submit data to a rails form via a URL? - ruby-on-rails

I have a basic search form in my rails app that records each search that has been entered. I also want my users to be able to initiate a search with a url in the browser. E.g. http://www.example.com/searches?q=foo
I've played around with the different routing options and the logic in my controller but I can't seem to get this form:
<%= form_for(#search) do |f| %>
<div class="field">
<%= f.label :search %><br />
<%= f.text_field :search %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
To initiate a POST action on the database and submit a new search.
Thanks :)

Change your form to do a GET request instead of a POST then you can use both the form and the url:
<%= form_for(#search), :method => :get do |f| %>

Related

Rails form submitted with GET request does not place querystring parameters in the URL

Working from the RailsGuides Action View Form Helpers article, in the "1.1 A Generic Search Form" section, using this code:
<% form_tag(search_path, :method => "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
When submitting this form, the resulting URL is '/search
', not '/search?q=searchterm', as would be expected.
The RailsGuides article even goes on to say,
Use "GET" as the method for search forms. This allows users to bookmark a specific search and get back to it. More generally Rails encourages you to use the right HTTP verb for an action.
However, it would be impossible to bookmark a specific search if the querystring parameter is not included in the URL. Why is this occurring, and what can be done about this?
try
`<%= form_with url: search_path, method: :get do |f| %>
<%= f.label :q, "Search for: " %>
<%= f.text_field :q %>
<%= f.submit :Submit%>
<% end %>`
form_with is an updated rails helper with rails 6

How to use form_for of rails in other controller?

I am currently writing a rails project. There are two model, course and post. What I want is to allow people to add comments(posts) in course's show page. I have a form_for for post, like this:
<%= form_for(#post) do |f| %>
<div class="well">
<h4>PICK a nickname:</h4>
<div class="form-group">
<%= f.text_field :user , class: "form-control", rows: "3"%>
</div>
<h4>Leave a Comment:</h4>
<div class="form-group">
<%= f.text_area :content , class: "form-control", rows: "3"%>
</div>
<%= f.submit "Add New Comment", class: "btn btn-primary" %>
</div>
<% end %>
How can I use this form_for in course's show html page?
Also, when submitting the post, I want the page to stay at the show page.
render as partial from course's show page (but don't forget put #post)
render partial '/comments/comment', locals: {post:post}
use remote true (with right method) / or redirect user back to
form_for post remote: true, method:'put/post/path'
for redirect back need to know your routes.rb

Rails 4 - Pre-Populate Form From Link

I have a form that allows users to manually input movies to review. Additionally, I am pulling movies from an external API (TMDB). When the user searches, it shows all results from the database as well as another area where data is pulled from the API. I want them to be able to click on a thumbnail from the external API and pre-populate my "new" form with that data. This code currently goes to the "new" form, but the title and description are not populated like I want them to be (a blank new form is rendered).
<% if #movies_api %>
<div class="row">
<% #movies_api.each do |movie| %>
<% if movie.poster_path %>
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<%= link_to(image_tag('https://image.tmdb.org/t/p/w396' + movie.poster_path, class: 'image', size:"400x600"), {:action => 'new', :controller => 'movies'}, :title => movie.title, :description => movie.overview ) if movie.poster_path %>
</div>
</div>
<% end %>
<% end %>
<% end %>
How do I accomplish pre-populating the new form with data I gather from the API?
I figured out a way to do this. In my form, I can do:
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title, :value => params[:title] %>
</div>
Not sure if there is a better way, but this works :-)

How to connect a file_field to the upload action in a rails app

I'm trying to add to a very simple data entry form, a button that allows the user to upload a file to the servers file system so that the url to that file can be placed in a field of the database.
So far, I've worked out that in the _form...erb file, I can add the file_field helper:
<%= form_for(#business) do |f| %>
<% if #business.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#business.errors.count, "error") %> prohibited this business from being saved:</h2>
<ul>
<% #business.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :deletedFlag %><br />
<%= f.check_box :deletedFlag %>
</div>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
...
<div class="field">
<%= f.label :logoURL %><br />
<%= f.text_field :logoURL %>
   <%= f.file_field :imagefile %>
</div>
<div class="field">
<%= f.label :streetAddress %><br />
<%= f.text_area :streetAddress %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This gives me a form with a "Choose file" button. Clicking on that allows the selection of the file. Great.
Now, from the ruby guide at: http://guides.rubyonrails.org/form_helpers.html#uploading-files I'm told to add something like:
def upload
uploaded_io = params[:business][:imagefile]
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'w') do |file|
file.write(uploaded_io.read)
end
end
to my controller.
What I haven't worked out is how that file_field gets connected to that 'upload' action. I can see in the logs that the button press accepts the filename, etc, but there's no indication that the file upload is started or attempted.
I've read a number of references, and they all seem to say that you need these two things, but (for me) they don't connect the dots. Yes I'm very new to Ruby/Rails, so I'm obviously missing something fundamental.
Did you forget to specify multipart in your form_for like this?
<%= form_for(#uploadfile, :html => {:multipart =>true}) do |f| %>
Check the generated form html code, do you see this?
<form ... enctype="multipart/form-data" ...>
You probably need to setup a route so that Rails can pass the request from your form into your controller, then setting up your form to use that route:
# config/routes.rb
post '/upload' => 'mycontroller#upload'
# app/views/some_model/new.html.erb
<%= form_for something, url: upload_path do |f| %>
...
This is probably the least recommended way of handling a file upload on Rails, especially for a new user, though. You would be much better off setting up Carrierwave, Paperclip, or Dragonfly instead.

Showing error messages inside of another controller's page

I'm current following the Rails Getting started guide, including creating a blog with post and comments models. In the post show method, there is a form to create a new comment. Like so.
<%= form_for([#post, #post.comments.build]) do |f| %>
<div class="field">
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
However, I want to prohibit users from posting more than one comment on a particular post. How would I go about doing this? I've been working with validators but I can't seem to wrap my head around adding errors and displaying them on the posts#show page.
The issue was actually in my controller. Since submissions are a nested resource of posts, I need to re-set #post before calling render 'posts/show' in case of an error. This works, however you'll notice that you'll end up on a URL such as /posts/4/submissions/ with the appropriate webpage, rather than /posts/4

Resources