This is my first question on StackOverflow. I am new to Rails and am making a simple Rails app in which I am doing a modal popup for user login in. My code is below.
App/Controller/Sessions:
class Users::SessionsController < Devise::SessionsController
# respond_to :html, :json
# before_action :check_user_session, only: [:new]
# GET /resource/sign_in
def new
self.resource = resource_class.new(sign_in_params)
clean_up_passwords(resource)
yield resource if block_given?
respond_to do |format|
format.js
format.html
end
end
# POST /resource/sign_in
def create
self.resource = warden.authenticate(auth_options)
if self.resource.present?
set_flash_message(:notice, :signed_in)
sign_in(resource_name, resource)
yield resource if block_given?
respond_with resource, location: after_sign_in_path_for(resource)
else
respond_to do |format|
format.js
end
end
end
My new.js.haml file:
$("#login-modal").html("#{escape_javascript(render 'new')}");
$("#exampleModal").modal();
I am getting this error when I click the sign in button.
change file new.js.haml to new.js.erb with following code:
$("#login-modal").html("<%= escape_javascript(render 'new') %>");
$("#exampleModal").modal();
Related
So, I'm trying to respond to an action with a js file.
def my_schedule
respond_to do |format|
format.js
end
end
In my view, I have 'my_schedule.js.erb' but it's not even executed, rails broke in the controller and throw me an error : ActionController::UnknownFormat, where I have the respond_to.
I tried to add
respond_to :js, :json, :html
at the beginning of my controller out of the actions but still not working.
Need help to debug this and understand how respond_to really works.
format.js will only respond to an xhr request. You can't trigger this response by just navigating to the route that points to this controller and method.
You can test the js.erb execution by changing the respond_to block to
def my_schedule
respond_to do |format|
format.html
format.js
end
end
Then create a my_schedule.html.erb file in the same view folder as the js.erb with the following contents
<%= link_to 'Test', my_schedule_path, data: { remote: true } %>
Note that you may need to adjust that path, I'm just guessing on that.
Then navigate to the same path you were trying to before. You should see a link which, when clicked, will fire the js response.
I am getting a "template missing error" on a .js.erb file, everything i've read says to add a respond_to which I did below but it's still not seeing my create.js.erb file, any ideas?
def create
#conversation = Conversation.find(params[:conversation_id])
#message = #conversation.messages.build(message_params)
#message.user_id = current_user.id
#message.save!
#path = conversation_path(#conversation)
respond_to do |format|
format.js
end
end
Seems like you don't have create.js.erb file in app/views/:controller_name/
If not create one at app/views/:controller_name/
My controller actions: advertisment_controller.rb
class AdvertismentsController < ApplicationController
before_action :set_advertisment, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!
skip_before_filter :verify_authenticity_token
layout 'admin'
respond_to :html
# GET /advertisments
# GET /advertisments.json
def index
#advertise_tab = 'active'
if current_user.is_super_admin?
#advertisments = Advertisment.all
elsif current_user.is_admin?
#advertisments = current_user.mediums.last.advertisments
elsif current_user.is_agent?
#advertisments = Advertisment.all.collect{|ad| ad if (ad.discount_rate.present? && ad.discount_rate.discount_provider == current_user.id) }.delete_if{|x| x == nil}
elsif current_user.is_member?
#advertisments = current_user.advertisments
end
end
# GET /advertisments/new
def new
if current_user.is_agent? || current_user.is_member?
#advertise_tab = 'active'
#advertisment = Advertisment.new
load_ad_components
else
redirect_to advertisments_path, flash: {error: 'You cannot create an advertisment'}
end
end
# GET /advertisments/1/edit
def edit
#mediums = Medium.all
#discount_rates = current_user.discount_rates.where(:media_category_id => #advertisment.media_category_id)
#draft_advertisments = current_user.advertisments.where(:draft => true)
respond_to do |format|
format.js
end
end
# POST /advertisments
# POST /advertisments.json
def create
#advertisment = current_user.advertisments.new(advertisment_params)
#advertisment.save
load_ad_components
respond_to do |format|
format.js
end
end
# PATCH/PUT /advertisments/1
# PATCH/PUT /advertisments/1.json
def update
#advertisment.update(advertisment_params)
load_ad_components
respond_to do |format|
format.js
end
end
# DELETE /advertisments/1
# DELETE /advertisments/1.json
def destroy
#advertisment.destroy
flash.now[:success] = "Advertisement deleted successfully."
load_ad_components
respond_to do |format|
format.js
end
end
end
My advertisments/_form.html.erb
<%= form_for(advertisment, :multipart => true, :remote => true) do |f| %>
// form fields
// advertisment is a local parameter
<% end %>
Now, if I submit this form, it is requesting the controller create method as html instead of js.
Also, for edit, I've created the link as
<li>Edit</li>
but when I click on the edit link nothing happens. No ajax call showing in the firebug console. My application.js file contains,
//= require jquery.min.js
//= require jquery_ujs
Whats wrong here? BTW, I've tested the whole edit, create, update, destroy with ajax rendering I've mentioned above in Chrome browser, and its working fine. Does anyone have any idea whats wrong with this code in firefox?
N.B. firebug for firefox show me that both the js files (jquery.min, jquery_ujs) are loaded. I've deleted my browser cache. But still the same. :'(
Any help will be appreciated.
I have a form which allows users to post an update. Once the users post the update I would like the list of updates to refresh. To achieve this I'm using Ajax and jQuery and using Rails. I'm running into trouble while trying to get jquery to render the post feed partial though.
Here's the jquery I'm using
$(".microposts").html("<%= j render partial: 'shared/feed_item', collection: #feed_items %>")
At the moment, the feed just refreshes and displays nothing. I believe it's due to the way I'm trying to pass the #feed_items. What's the best way to pass that variable?
Someone asked for the controller;
class MicropostsController < ApplicationController
before_action :signed_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
#micropost = current_user.microposts.build(micropost_params)
if #micropost.save
respond_to do |format|
format.html { redirect_to root_url }
format.js
end
else
#feed_items = []
flash[:error] = "Failed to create micropost!"
render 'static_pages/home'
end
end
def destroy
#micropost.destroy
flash[:success] = "Micropost deleted!"
redirect_to root_url
end
private
def micropost_params
params.require(:micropost).permit(:content)
end
def correct_user
#micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if #micropost.nil?
end
end
#feed_items needs to be defined somewhere in the controller. The # is a special symbol in Ruby that signifies an instance variable of the current class. If you define it somewhere else, it becomes an instance variable of that class.
Rails has some special magic that makes the instance variables of the controller available in the view. If it's not an instance variable on the controller, it won't work.
def create
#micropost = current_user.microposts.build(micropost_params)
if #micropost.save
#feed_items = #micropost.do_whatever_to_build_the_feed_items
respond_to do |format|
format.html { redirect_to root_url }
format.js
end
else
#feed_items = []
flash[:error] = "Failed to create micropost!"
render 'static_pages/home'
end
end
I'm using the jQuery plugin Raty for my ruby on rails 4.0 application and it seems to be working except the images for the stars aren't loading onto the screen.
So those images should be stars ^ When I mouseover them in the console while running web brick the following is outputted. (My scaffold is called reviews)
Started GET "/reviews/star-off.png" for 127.0.0.1 at 2014-03-28 14:15:42 -0400
Processing by ReviewsController#show as PNG
Parameters: {"id"=>"star-off"}
Review Load (1.3ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."id" = $1 LIMIT 1 [["id", "star-off"]]
Completed 404 Not Found in 2ms
ActiveRecord::RecordNotFound (Couldn't find Review with id=star-off):
app/controllers/reviews_controller.rb:67:in `set_review'
I currently have the star images in apps/assets/javascripts/images but have also tried to put them in app/assets/images and app/views/reviews but they still won't show up. Is my issue that they aren't in the correct directory (and if so, which directory should they be in) or do I need to add some code manually to my reviews controller? Thanks.
edit: So when I try to use it in my index page, I only get an error saying this, so I must have to do something to my routes.rb file?
ActionController::RoutingError (NNo route matches [GET] "/star-off.png")
edit: as requested here is routes.rb
ConcertReview::Application.routes.draw do
resources :reviews
get "review/index"
get "review/artist"
get "review/date"
get "review/venue"
get "review/genre"
get "review/comments"
root 'reviews#index'
get 'reviews/' => 'reviews#index'
end
and here's reviews_controller.rb (autogenerated from scaffold and have not modified)
class ReviewsController < ApplicationController
before_action :set_review, only: [:show, :edit, :update, :destroy]
# GET /reviews
# GET /reviews.json
def index
#reviews = Review.all
end
# GET /reviews/1
# GET /reviews/1.json
def show
end
# GET /reviews/new
def new
#review = Review.new
end
# GET /reviews/1/edit
def edit
end
# POST /reviews
# POST /reviews.json
def create
#review = Review.new(review_params)
respond_to do |format|
if #review.save
format.html { redirect_to #review, notice: 'Review was successfully created.' }
format.json { render action: 'show', status: :created, location: #review }
else
format.html { render action: 'new' }
format.json { render json: #review.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reviews/1
# PATCH/PUT /reviews/1.json
def update
respond_to do |format|
if #review.update(review_params)
format.html { redirect_to #review, notice: 'Review was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #review.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reviews/1
# DELETE /reviews/1.json
def destroy
#review.destroy
respond_to do |format|
format.html { redirect_to reviews_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_review
#review = Review.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def review_params
params.require(:review).permit(:artist, :venue, :date, :genre, :sound, :stagePresence, :songSelection, :overallRating, :comments)
end
end
I had that very same problem on my project. The star images do belong in app/assets/images like you tried. However, you need to pass raty a path option like such:
$('div').raty({
readOnly: true,
halfScore: true,
score: 3,
path: '/assets'
});
This is working for me.
Also, if you have turbolinks on, you're going to have to tweek any $(document).ready(function () {...}); you might have. That was the problem I had right after this one.