I am trying to use the best_in_place gem with a Rails 4 app. I can
get it to update valid edits to a field fine, but if I enter an
invalid value, I don't see error messages. I have added the .purr
styling rules, but still no joy.
I use the following in the controller:
def update
#transact = Transact.find(params[:id])
respond_to do |format|
if #transact.update_attributes(transact_params)
flash[:notice] = 'Transaction was successfully updated.'
format.html { redirect_to(#transact) }
format.xml { head :ok }
format.json { respond_with_bip(#transact) }
else
#errors = #transact.errors
format.html { render :action => "edit" }
format.xml { render :xml => #transact.errors, :status => :unprocessable_entity }
# format.json {
# render :json => #errors.full_messages,
# :status => :unprocessable_entity
# }
format.json { respond_with_bip(#transact) }
end
end
end
And I've also tried the commented-out code in the above, with similar results.
Here is what the server responds on an invalid value:
Processing by TransactsController#update as JSON
Parameters: {"transact"=>{"shares"=>"6741433.0x"}, "authenticity_token"=>"e+1ZEhVYuEMDURf81Kcxg0Ld28BfY60rRFRSZUq8RsY=", "id"=>"144314"}
Transact Load (0.5ms) SELECT "transacts".* FROM "transacts" WHERE "transacts"."id" = $1 LIMIT 1 [["id", "144314"]]
(0.1ms) BEGIN
(0.3ms) ROLLBACK
Completed 422 Unprocessable Entity in 60ms (Views: 0.2ms | ActiveRecord: 0.9ms)
Is there any thing obvious I am doing wrong?
I had the same problem. My solution was to include ALL these three lines in application.js:
//= require best_in_place
//= require best_in_place.purr
//= require jquery.purr
If jquery.purr or best_in_place.purr was left out, no error message was displayed to the user.
For the record, see my comment above: I was not including best_in_place.purr.js in my javascript assets in addition to best_in_place.js.
Hope it helps someone else overlooking this.
I considered this answered.
Related
I'm trying to implement a simple rails fullstack todo app with action cable.
Unfortunately the docs on action cable is still confusing to me.
I tried to refer to DHH's example and https://blog.heroku.com/real_time_rails_implementing_websockets_in_rails_5_with_action_cable and adjusting to my app https://github.com/tenzan/rails-fullstack-todo
It's not showing realtime updates at http://localhost:3000/tasks when I used one browser for task creation and another one to see if it changes automatically.
My environment:
Mac OS X Sierra
Rails 5.0.0.1
Ruby 2.4.0
Redis enabled in Gemfile and running at port 6379
RethinkDB
I implemented:
app/assets/javascripts/channels/index.coffee
//= require cable
//= require_self
//= require_tree .
this.App = {};
App.cable = ActionCable.createConsumer();
app/assets/javascripts/channels/tasks.js
App.tasks = App.cable.subscriptions.create('TasksChannel', {
received: function(data) {
$("#tasks").removeClass('hidden')
return $('#tasks').append(this.renderTask(data));
}
});
app/channels/tasks_channel.rb
class TasksChannel < ApplicationCable::Channel
def subscribed
stream_from "tasks"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
app/controllers/tasks_controller.rb
def create
#task = Task.new(task_params)
respond_to do |format|
if #task.save
ActionCable.server.broadcast 'tasks', task: #task.title
format.html { redirect_to tasks_url, notice: 'Task was successfully created.' }
format.json { render :show, status: :created, location: #task }
else
format.html { render :new }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
app/views/layouts/application.html.erb: Added <%= action_cable_meta_tag %>
config/cable.yml
local: &local
url: redis://localhost:6379
development: *local
test: *local
config/environments/development.rb
Rails.application.configure do
config.action_cable.url = "ws://localhost:3000/cable"
config/routes.rb
Rails.application.routes.draw do
mount ActionCable.server => '/cable'
app/views/tasks/index.html.erb
I suspect something wrong with
app/assets/javascripts/channels/tasks.coffee
app/views/tasks/index.html.erb
But I really don't have an idea how to fix/adjust them.
I'm working on building out my mailer, but I keep running into:
wrong number of arguments (0 for 1)
Call my crazy, but I feel like I defined everything correctly:
Controller (truncated for brevity):
def create
#cms484 = Cms484.new(cms484_params)
respond_to do |format|
if #cms484.save
SendLink.message(#cms484).deliver_later
format.html { redirect_to cms484s_path, notice: 'Cms484 was successfully created.' }
format.json { render :show, status: :created, location: #cms484 }
else
format.html { render :new }
format.json { render json: #cms484.errors, status: :unprocessable_entity }
end
end
SendLink.rb:
class SendLink < ApplicationMailer
def message(cms484)
#cms484 = cms484
mail(
:subject => 'Hello from Postmark',
:to => #cms484.recipient ,
:from => 'info#mysite.com',
:html_body => '<strong>Hello</strong> user!.',
end
end
Can anybody else see the needle in the haystack or am I missing something else entirely?
I'm using Postmark for delivery if that matters, and have those parameters defined in my application.rb file as per the documentation. Think this is a simpler matter though.
Edit
The complete error:
Completed 500 Internal Server Error in 76ms
ArgumentError (wrong number of arguments (0 for 1)):
app/mailers/send_link.rb:2:in `message'
app/mailers/send_link.rb:4:in `message'
app/controllers/cms484s_controller.rb:38:in `block in create'
app/controllers/cms484s_controller.rb:36:in `create'
I had a similar issue where I named my ActionMailer method "message" it turns out it was a reserved word in Rails and threw an error.
I would assume that "mail" was a reserved word where "email" was not.
mail ... line in SendLink.rb looks wrong , change it to,
mail(
:subject => 'Hello from Postmark',
:to => #cms484.recipient ,
:from => 'info#mysite.com',
:html_body => '<strong>Hello</strong> user!.')
Ok, so I decided to re-write it and behold - it works. Why or what's different than the previous version(other than the method email vs mail, surely that can't be it?), I have no idea. If you can see what it is, Please point it out to me!
Send_link.rb:
class SendLink < ApplicationMailer
def email(cms484)
#cms484 = cms484
mail(
:subject => 'Hello from Postmark',
:to => #cms484.recipient ,
:from => 'info#mysite.com',
)
end
end
Controller:
def create
#cms484 = Cms484.new(cms484_params)
respond_to do |format|
if #cms484.save
SendLink.email(#cms484).deliver_later
format.html { redirect_to cms484s_path, notice: 'Cms484 was successfully created.' }
format.json { render :show, status: :created, location: #cms484 }
else
format.html { render :new }
format.json { render json: #cms484.errors, status: :unprocessable_entity }
end
end
end
I'm scaffolding with rails, and the following code is being generated
format.json { render json: #leg, status: :created, location: #leg }
But RubyMine is complaining unless I switch it to
format.json { render :json => #leg, :status => :created, :location => #leg }
Can anyone elaborate?
http://youtrack.jetbrains.com/issue/RUBY-12466 suggests that you set the SDK to ruby 1.9 to get this working.
First, I am really sorry if this question is too trivial. I am new with rails and couldn't figure out where i am doing it wrong.
I have a model named Costing and in it's index page i have a search form. I am trying to use 'axlsx' gem to download only the search results but I always get all the rows. I am also using 'will_paginate' gem.
Here is my code.
//costings_controller.rb
def index
#costings = Costing.search(params[:search] , params[:page])
respond_to do |format|
format.html # index.html.erb
format.json { render json: #costings }
format.xlsx {
send_data #costings.to_xlsx.to_stream.read, :filename => 'costings.xlsx', :type => "application/vnd.openxmlformates-officedocument.spreadsheetml.sheet"
}
end
end
// index.html.erb
<%= link_to 'Download Costings', url_for(:format=>"xlsx") %>
Please help me here.
Thanks a lot in advance.
Here is the code that I made for a demo of axlsx gem. Browse through it and implement your requirement in the controller. Here is the output of this demo.
//costings_controller.rb
def download
#costings = Costing.search(params[:search] , params[:page])
respond_to do |format|
format.html # index.html.erb
format.json { render json: #costings }
format.xlsx {
send_data #costings.to_xlsx.to_stream.read, :filename => 'costings.xlsx', :type => "application/vnd.openxmlformates-officedocument.spreadsheetml.sheet"
}
end
end
// index.html.erb
<%= form_for :costing, url: download_costing_index_path do %>
...
<% end %>
//routes
resources :costing do
collection do
post :download, :defaults => { :format => 'xlsx' }
end
end
Trying to build a CMS for a blog using rails 3.
In my routes.rb...
namespace :admin do
resources :posts
root :to => "home#index"
end
In Admin::PostsController...
def update
#post = Post.find(params[:id])
respond_to do |format|
if #post.update_attributes(params[:post])
format.html { redirect_to(#post,
:notice => 'Post was successfully updated.')}
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #post.errors,
:status => :unprocessable_entity }
end
end
end
I had to change the first line of admin/_form.html.erb due to a previous 'undefined method' error that was driving me crazy. Was trying to point the browser to a nonexistent "post_path".
<%= form_for(#post, :url => admin_posts_path(#post)) do |f| %>
All other methods for posts are working as expected. Upon form submission (update) - the rails server...
Started POST "/admin/posts.1"
ActionController::RoutingError (No route matches "/admin/posts.1"):
First, curious as to why it is using POST instead of PUT for the update.
Second, I can't figure out why the URL is being interpreted as "/admin/posts.1" and how to fix it.
Has anyone else run into this problem? (and yes, I am following the rubyonrails.org guides closely to help me). Any help would be greatly appreciated.
EDIT:
Changed admin_posts_path(#post) to admin_post_path(#post) per theIV.
the rails server...
NoMethodError (undefined method 'post_url' for #<Admin::PostsController:0x00000102b26ff8>):
app/controllers/admin/posts_controller.rb:55:in 'block (2 levels) in update'
app/controllers/admin/posts_controller.rb:53:in 'update'
I believe you should be hitting admin_post_path(#post), not admin_posts_path(#post).
Look at the table that lists all of the helpers created for your routes on guides.rubyonrails.org.
EDIT: Also, have you tried the array style of urls? It's pretty convenient.
<%= form_for([:admin, #post]) do |f| %>
EDIT 2: My guess as to "undefined method post_url" is from your update action here:
format.html { redirect_to(#post,
:notice => 'Post was successfully updated.')}
It needs to be namespaced as well:
format.html { redirect_to([:admin, #post],
:notice => 'Post was successfully updated.')}