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.
Related
I'm trying to sign up Users using Devise, and render a JS file if a certain parameter is included in the sign-up. I'm getting the following error:
ActionController::UnknownFormat in RegistrationsController#create
ActionController::UnknownFormat
This is my "create" action (I marked the line the error pointed to):
if resource.persisted?
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_flashing_format?
sign_up(resource_name, resource)
if !params[:name].nil?
View.create(name: params[:name])
respond_to do |format| #THIS IS THE LINE WITH THE ERROR
format.js
end
else
respond_with resource, location: after_sign_up_path_for(resource)
end
else
respond_with resource, location: inactive_path_for(resource)
end
else
respond_with resource
end
The action is "create", in app/controllers/registrations_controller.rb. The JS file I'm trying to render is app/views/devise/registrations/create.js.erb.
My suspicion is that there's a problem with finding the file, but I can't figure out how to point it to the right route, or if that's even the right problem. Anyone have any thoughts?
New to Rails so I apologize if this is a silly question.
I currently have an application in Rails that renders JSON output to javascript framework 'jsTree', but I am only able to get one 'tree' worth of information because I am creating, deleting, etc. the nodes themselves in the tree using ajax pointed at the correct route.
I would like to be able to have a function that creates an entirely new set of JSON data for use with other individual trees (stated another way, upon creation of an object with a view containing the jsTree client-side code in the view, I would like to associate different JSON information with that object/tree).
If someone could explain the logic of how I might go about this or point me in the direction of a tutorial/other resources, that would be very helpful.
Controller for the JSON
class JsonsController < ApplicationController
before_action :set_json, only: [:show, :update, :destroy]
# GET /jsons
# GET /jsons.json
def index
#jsons = Json.all
render json: #jsons
end
# GET /jsons/1
# GET /jsons/1.json
def show
render json: #json
end
# POST /jsons
# POST /jsons.json
def create
#json = Json.new(json_params)
if #json.save
render json: #json, status: :created, location: #json
else
render json: #json.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /jsons/1
# PATCH/PUT /jsons/1.json
def update
#json = Json.find(params[:id])
if #json.update(json_params)
head :no_content
else
render json: #json.errors, status: :unprocessable_entity
end
end
# DELETE /jsons/1
# DELETE /jsons/1.json
def destroy
#json.destroy
head :no_content
end
private
def set_json
#json = Json.find(params[:id])
end
def json_params
params.require(:json).permit(:text, :parent, :id)
end
end
Each dashboard object should have its own set of JSON but only one is created for the entire application at the moment via the controller above.
class DashboardsController < ApplicationController
before_action :authenticate_user!
# Requires user to be signed in
def index
#dashboards = Dashboard.all
end
def new
#dashboard = Dashboard.new
end
def edit
#dashboard = Dashboard.find(params[:id])
end
def create
#dashboard = Dashboard.new(dashboard_params)
#dashboard.save
if #dashboard.save
redirect_to :action => :index
else
render :action => new
end
end
def update
#dashboard = Dashboard.find(params[:id])
if #dashboard.update(dashboard_params)
redirect_to :action => :index
else
render 'edit'
end
end
def show
#dashboard = Dashboard.find(params[:id])
end
private
def dashboard_params
params.require(:dashboard).permit(:title, :description)
end
end
I'm trying to implement this Angularjs upload in my rails application:
Angular File upload
This is my photos_controller:
def create
#photo = current_user.photos.new(photo_params)
respond_to do |format|
if #photo.save
format.html { redirect_to #photo, notice: 'Photo was successfully created.' }
format.json { render action: 'show', status: :created, location: #photo }
else
format.html { render action: 'new' }
format.json { render json: #photo.errors, status: :unprocessable_entity }
end
end
end
def photo_params
params.require(:photo).permit(:album_id, :user_id, :title, :description, :path)
end
I think that I'm close of the solution. When I try to upload I get this error:
param is missing or the value is empty: photo
Extracted source (around line #92):
9091929394
# Never trust parameters from the scary internet, only allow the white list through.
def photo_params
params.require(:photo).permit(:album_id, :user_id, :title, :description, :path)
end
end
I think that I need to format the data send to a format like this, isn't?
{"photo"=>{"tmpname"=>"253", "etc"=>"1"}}
The terminal log:
my html input:
<input type="file" nv-file-select="" name="photo[path]" id="photo_path" uploader="uploader" multiple /><br/>
My column with paper clip is path
I'm not getting how I do this in this angular script.
I don't have the rep to comment yet but usually this error comes from a naming issue, make sure your fields are named following the rails convention for example "photo[path]"
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
In the "Working With Javascript" section of the Rails Edge Guides, an example is given of how to structure a 'create' action inside a 'User' controller, in order to integrate AJAX into the creation action:
# app/controllers/users_controller.rb
# ......
def create
#user = User.new(params[:user])
respond_to do |format|
if #user.save
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.js {}
format.json { render json: #user, status: :created, location: #user }
else
format.html { render action: "new" }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
I've read here that both 'format.js' and 'format.json' are needed, because Javascript and JSON are different types of response. My question is, if both formats are specified in the first half of the 'create' action's if statement, why aren't both also needed in the 2nd half? Does this mean that Javascript uses the 'create.js.erb' file on success, but not on failure?
Yes, you are correct. Based on skimming the guide, it looks like you don't want to do anything with js in the case of failure. If you were to put
format.js {}
in the failure block as well, then it would try to execute the render #user portion, which would probably cause an error anyway.
If you really did want to execute a js block in the event of failure, you could just do it the same way as for the other formats.