Rails unknown format on AJAX - ruby-on-rails

I'm trying to implement a very simple file form using the remotipart gem. Most of my files are the exact same as the tutorial ones:
timeline.html.erb :
<%= form_tag new_feed_path(:format => "js"), remote: true, :html => { :multipart => true } do |f| %>
<%= hidden_field_tag :brief_id, #brief.id %>
<%= file_field_tag :file %>
<%= submit_tag "Send", class: "btn btn-success" %>
<% end %>
briefs_controller.rb
def new_feed
puts params
respond_to do |format|
format.js
end
end
new_feed.js.erb
alert('success!');
<% if remotipart_submitted? %>
alert('submitted via remotipart')
<% else %>
alert('submitted via native jquery-ujs')
<% end %>
But everytime I submit the form, I get the following error in the logs:
Processing by ResourcesController#create as HTML
Completed 406 Not Acceptable in 14ms
ActionController::UnknownFormat - ActionController::UnknownFormat:
Did I miss something ? I know ajax file upload can be tricky on RoR, but remotipart seems to be a viable solution.
EDIT I managed to fix the first issue by adding :format => "js" , but now I face another problem: none of the form datas are sent. In fact, here are the sent params:
{"controller"=>"briefs", "action"=>"new_feed"}

Check out the example from Remotipart's docs.
Looks like you're not passing :html => { :multipart => true } to form_for

try it
<%= form_tag new_feed_path, html: {multipart: true }, method: :post, remote:true do |f| %>
<%= hidden_field_tag :brief_id, #brief.id %>
<%= file_field_tag :file %>
<%= submit_tag "Send", class: "btn btn-success" %>
<% end %>
edit
try it
install this gem pry
RailsCast Pry
briefs_controller.rb
def new_feed
binding.pry #just to be sure that this action is not called
puts params
respond_to do |format|
format.js { render 'new_feed') # modify this
end
end

Related

Generate form for with csv format

I am trying to use form for to specify two date fields, which specifies a timespan for data that I want to put inside a CSV file. I am able to create my csv file but I cant get rails to respond to csv format.
Here I have added my format: :csv in the form_for field, but it wont respond to that format in my controller.
<%= form_for(:estimation, html: {class: "estimations-form-horizontal"}, url: research_path, format: :csv, method: :get) do |f| %>
<div class="form-group-estimations">
<%= f.label :from_start_date, "Från och med" %>
<%= f.date_field :from_start_date, class: "form-control" %>
</div>
<div class="form-group-estimations">
<%= f.label :to_end_date, "Till och med" %>
<%= f.date_field :to_end_date, class: "form-control" %>
</div>
<%= f.submit "Hämta data", class: "btn btn-primary" %>
<% end %>
My controller, using format.csv ...
def research
if (params[:estimation] && params[:estimation][:from_start_date] && params[:estimation][:to_end_date])
start_date = params[:estimation][:from_start_date].to_date.beginning_of_day
end_date = params[:estimation][:to_end_date].to_date.end_of_day
if(start_date > end_date)
puts "Dates are in wrong order"
else
#estimations = Estimation.where(:created_at => start_date..end_date)
puts to_csv(#estimations)
respond_to do |format|
format.csv { send_data to_csv(#estimations), filename: "skattningar-#{Date.today}.csv" }
end
end
else
puts "Not enough data"
end
end
:url conflicts with :format If you are passing both :url and :format,
url overwrites the use of format, so you’ll need to pass it in the url
like so:
form_for user, :url => user_path(#user, :format => :json)
Source

How to do the calculation without any models in Rails?

I need to get an integer(#integer) from the form in my root_path, do multiplication (#integer*45) and display the result on the same page. How can I do it without any models in my application?
Please, share your best practice. Thank you!
I was trying to do next:
CalculatorsController
def calculation
#integer = params[:integer]
#result = #integer*45
end
def result
end
root.rb
root :to => 'calculators#result'
resources :calculators, :collection=>{:result => :get, :calculation => :post}
calculators/result.html.erb
<% form_tag root_path, :html => {:method => :post} do %>
<%= label_tag 'integer' %>
<%= text_field_tag :integer %>
<div><%= submit_tag 'OK' %></div>
<% end %>
I'll do it with ajax, so there is no need for page refresh:
First, update the routes, for your example you only need two routes, one get (or root) and one post.
routes.rb:
Rails.application.routes.draw do
root 'calculators#result'
post 'calculators/calculation'
end
Next, update your view:
Change the url in your form_tag where the data will be sent (to calculation action instead of result).
Add remote: true option to enable ajax.
Add a tag where you will display your result.
result.html.erb:
<% form_tag calculators_calculation_url, remote: true do %>
<%= label_tag 'integer' %>
<%= text_field_tag :integer %>
<div><%= submit_tag 'OK' %></div>
<% end %>
<div id="total"></div>
And create a view for calculation action, but since you are using ajax, you will create it as js.erb and include the required javascript (or jQuery) to update your view (i'm using jQuery in the example).
calculation.js.erb:
$('#total').html('<%= #result %>')
Now when you click submit, your form will be sent to calculation action and will update the div with #result.
Just add the field to your form...
<% form_tag root_path, :html => {:method => :post} do %>
<%= label_tag 'integer' %>
<%= text_field_tag(:integer, #integer) %>
<% if #result.present? %>
<br>
Result is: <%= #result %>
<br/>
<% end %>
<div><%= submit_tag 'OK' %></div>
<% end %>
And then render result in your calculate...
def calculation
#integer = params[:integer].to_i
#result = #integer*45
render :result
end
Your result view (result.html.erb) is getting its data from the result method, not calculation. Update your controller as follows:
def calculation
#integer = params[:integer]
end
def result
#result = #integer*45
end
You then need a tag to display your result in the view, something like:
<p> <%= #result %> </p>

Routing Error: uninitialized constant for nonexistent controller

Upon checking submit I get Routing Error: uninitialized constant DuelersController
duels/show.html.erb
The loser(s) will <%= #duel.consequence %><br><br>
If everyone succeeds they will <%= #duel.reward %>
<%= form_for #dueler do |f| %>
Accept? <%= f.check_box :accept %>
<%= f.submit %>
<% end %>
There is no DuelersController only DuelsController
def show
#dueler = Dueler.find_by(user_id: current_user.id)
respond_with(#duel)
end
def set_duel
#duel = Duel.find(params[:id])
end
Once a user clicks submit how can I redirect the user back to the show page?
Dueler.last
id: 20,
user_id: 78,
challenge_id: 178,
duel_id: 13,
accept: nil> # For example redirect back to duels/13 with accept: true
Sorry, I was distracted. I think your problem is actually another, you have to explicitly specify the controller and action here.
On a side note, maybe you'll have to change the way you fetch your parameters on the set_duel action (I don't remember if rails sets the id automatically), anyway:
#Assuming you want to call set_duel upon submission
<%= form_for #dueler, :url => { :controller => "duel", :action => "set_duel" }, :html => {:method => :post} do |f| %>
Accept? <%= f.check_box :accept %>
<%= f.submit %>
<% end %>

"406 Not Acceptable" for file upload in RoR with AJAX

Tis is my view:
<%= form_for item, :url => comment_item_path(item), :html => {:remote => true, 'portal-transform' => true, :multipart => true} do |f| -%>
<%= f.fields_for :updates, Update.new, :index => nil do |m| -%>
<%= m.text_area :comment %><br />
<%= m.file_field :attachment %>
<% end -%>
<%= f.submit "Comment" %>
<% end -%>
And controller action:
respond_to do |format|
format.js do
render :json => {}
end
end
When I submit the form with only comment (text_area) field entered and keep attachment (file_field) field blank, it render exactly what expected.
But when I submit the form with attachment, it resulted in:
Completed 406 Not Acceptable in 56ms
What went wrong for me? Please guide.
Thanks.
Browsers do not allow file uploads via AJAX for security reasons. If you leave the form's file_field blank however, the form submits normally with no error, which explains the behaviour you are seeing.
To upload files via AJAX in Rails 3, you can use the Remotipart gem.
http://os.alfajango.com/remotipart/
Here is an example usage:
http://thechangelog.com/post/7576700785/remotipart-rails-3-ajax-file-uploads-made-easy

Rails / Paperclip: file_field not displaying

I'm attempting to use PaperClip and have been following the quick start found on their GitHub page.
I've got the gem install, model & controller set up, and in the view I have:
<% form_for #account, :html => { :multipart => true } do |f| %>
<%= f.file_field :image %>
<% end %>
But nothing displays when the page renders. I've stripped out all CSS to ensure that isn't an issue, but still not luck. Any thoughts on why the file picker isn't displaying? Thanks.
Not sure what version of Rails you are using but I think you forgot the equals sign in the form helper, try:
<%= form_for #account, :html => { :multipart => true } do |f| %>
<%= f.file_field :image %>
<% end %>

Resources