I have 2 different post types (news and articles). When i go to news and try to comment any news, nothing happen. In google chrome console it says:
POST /news/artifact-v-spiske-liderov-prodazh-steam-po-itogam-2018-goda/comments/ 404
rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:7
Rails.ajax # rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:7
Rails.handleRemote # rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:31
(anonymous) # rails-ujs.self-d109d8c5c0194c8ad60b8838b2661c5596b5c955987f7cd4045eb2fb90ca5343.js?body=1:5
BUT! When i try comment in Articles type, all good, comments work. What is problem?
My _form.html.haml:
- if user_signed_in? && current_user.mute_to && current_user.mute_to > Time.current
%div
%strong
= "Вы не можете писать комментарии до #{current_user.mute_to.strftime("%F %R")}"
- elsif user_signed_in?
= form_with model: [commentable, Comment.new], html: { class:
local_assigns[:class], data: { target: local_assigns[:target] } } do |form|
.form-group
= form.text_area :body, placeholder: "Напишите комментарий (минимум 3 символа)", class: "form-control"
.form-group
= form.hidden_field :parent_id, value: local_assigns[:parent_id]
= form.submit 'Отправить', class: "btn"
My _comment.html.haml:
- nesting = local_assigns.fetch(:nesting, 1)
- max_nesting = local_assigns[:max_nesting]
- continue_thread = local_assigns[:continue_thread]
= tag.div id: dom_id(comment), class: "border-left pl-4 my-4" do
- if comment.deleted?
%strong [комментарий удален]
%small
= link_to time_ago_in_words(comment.created_at), url_for(comment: comment.id, anchor: dom_id(comment))
%p [комментарий удален]
- else
- comments_user = comment.user
%strong.comments_user_line
= fa_icon "user-circle", class: "mr-1"
= link_to comments_user.nickname, user_path(comments_user), class: 'user_link'
%small
= link_to time_ago_in_words(comment.created_at), polymorphic_path(comment.commentable, comment: comment.id, anchor: dom_id(comment))
= simple_format comment.body
%div.comments_block
- if user_signed_in? && current_user.mute_to && current_user.mute_to > Time.current
- else
%small
- if user_signed_in?
%btn.reply Отвеить
- else
= link_to "Отвеить", new_user_session_path
= link_to "Удалить", [comment.commentable, comment], method: :delete, data: {confirm: "Вы уверены?"} if comment.user == current_user
= render partial: "comments/form", locals: { |
commentable: comment.commentable, |
parent_id: reply_to_comment_id(comment, nesting, max_nesting), |
class: "mt-4 d-none replies_form", |
target: "reply.form" |
} |
= tag.div id: "#{dom_id(comment)}_comments" do
- if continue_thread.present? && nesting >= continue_thread && comment.comments.any?
= link_to "Открыть ветку", url_for(comment: comment.id, anchor: dom_id(comment))
- else
= render comment.comments, continue_thread: continue_thread, nesting: nesting + 1, max_nesting: local_assigns[:max_nesting]
My comments_controller.rb:
class CommentsController < ApplicationController
before_action :authenticate_user!
def create
#comment = #commentable.comments.new(comment_params)
#comment.user = current_user
if #comment.save
respond_to do |format|
format.html { redirect_to #commentable }
format.js
end
else
redirect_to #commentable, alert: "Ошибка :("
end
end
def destroy
#comment = #commentable.comments.find(params[:id])
return false unless current_user.id == #comment.user_id
#comment.destroy
redirect_to #commentable
end
private
def comment_params
params.require(:comment).permit(:body, :parent_id)
end
end
If i uncomment rails-ujs in application.js like this:
require rails-ujs
Then when i posting comment it will be say:
ActiveRecord::RecordNotFound in News::CommentsController#create
Couldn't find News with 'id'=artifact-v-spiske-liderov-prodazh-steam-po-itogam-2018-goda
Extracted source (around line #7):
6 def set_commentable
7 #commentable = News.find(params[:news_id])
8 end
9 end
How i can fix it? Thanks!
The first suggestion - comment require rails-ujs until you solve the issue. It will be easier to debug.
Then, the issue is that as a params[:news_id] you are passing news slug (artifact-v-spiske-liderov-prodazh-steam-po-itogam-2018-goda) instead of news id. One solution is to modify the code to pass news id as params[:news_id].
Another, a little big uglier solution, is to refactor set_commentable method in the News::CommentsController to something like this:
def set_commentable
#commentable = News.find_by(slug: params[:news_id])
end
However, it might break other places where set_commentable is used.
Related
I have page with table of categories. I'm using SmartListing GEM for sorting data and also I added some buttons for each item in table, including "delete" button.
Problem: after element in the table was deleted, table blinks but doesn't refresh data, so I need to reload hole page to get updated table. (By the way, element removed from the database without problems)
My files look like the files in developer example.
My files:
index and destroy actions in CategoriesController
def index
#categories = Category.all
smart_listing_create :categories, #categories,
sort_attributes: [
[:created_at, 'categories.created_at'],
[:updated_at, 'categories.updated_at'],
[:name, 'categories.name'],
[:calculate, 'categories.issues_count']
],
default_sort: { name: 'asc' }, partial: 'category'
end
def destroy
#category = Category.find(params[:id])
if #category.issues.empty?
#category.destroy
flash[:success] = 'Deleted'
else
flash[:alert] = 'Category isn\'t empty!'
end
end
index.html.slim
#heading-breadcrumbs
.container
.row
.col-md-7
h1=title 'Categories'
.col-md-5
ul.breadcrumb
li= link_to 'Main', root_path
li Categories
#content
.container
.row
#categories-moderation.col-md-12.col-sm-12.col-xs-12
= link_to 'Create new', new_account_admin_category_path, class: "newcategory btn btn-lg btn-info"
= smart_listing_render(:categories)
_category.html.slim
- unless smart_listing.empty?
.table-responsive
table.table.table-hover.flex-table
thead
tr.centered
th.col-md-2.col-sm-3
= smart_listing.sortable 'Created', :created_at
th.col-md-2.col-sm-3
= smart_listing.sortable 'Updated', :updated_at
th.col-md-4.col-sm-3
= smart_listing.sortable 'Title', :name
th.col-md-2.col-sm-2.hidden-title
= smart_listing.sortable 'Issues', :calculate
th.col-md-2.col-sm-2.hidden-title
| Actions
tbody
- smart_listing.collection.each do |category|
tr.editable[data-id=category.id]
= smart_listing.render object: category, partial: 'category_item', locals: {object: category}
= smart_listing.paginate
- else
p Empty
_category_item.html.slim
tr.centered
td.col-md-2.col-sm-3.col-xs-12.flex-order-0
span.user-label Created:
= object.created_at.strftime("%m.%d.%Y, %T")
td.td.col-md-2.col-sm-3.col-xs-12.flex-order-1
span.user-label Updated:
= object.updated_at.strftime("%m.%d.%Y, %T")
td.lefted.col-md-4.col-sm-2.col-xs-12.flex-order-2
= link_to object.name, object
td.issues_count.col-md-2.col-sm-5.col-xs-12.flex-order-3
span.user-label Issues:
= render partial: 'shared/number_of_issues', locals: { id: object.id }
td.actions.col-md-4.col-sm-5.col-xs-12.flex-order-4
= smart_listing_item_actions [{name: :edit,
url: edit_account_admin_category_path(object),
icon: 'glyphicon glyphicon-pencil',
title: 'Edit'},
{name: :destroy,
url: account_admin_category_path(object),
confirmation: 'Sure?',
icon: 'glyphicon glyphicon-trash',
remote: true, if: object.issue_ids.empty?,
title: 'Delete'}]
index.js.erb
<%= smart_listing_update(:categories) %>
update.js.erb
<%= smart_listing_item :categories, :update, #category, 'category_item' %>
destroy.js.erb
<%= smart_listing_item :categories, :destroy, #category %>
Problem: after element in the table was deleted, table blinks but doesn't refresh data, so I need to reload hole page to get updated table. (By the way, element removed from the database without problems)
I found a solution to the problem. It was resolved simply - just had to add to the destroy action line redirect_to action: 'index', status: 303, so this action should look like this:
def destroy
#category = Category.find(params[:id])
if #category.issues.empty?
#category.destroy
flash[:success] = 'Deleted'
redirect_to action: 'index', status: 303
else
flash[:alert] = 'Category isn\'t empty!'
end
end
I know this kind of question is already answered multiple times but i seriously unable to figure it out what is causing a problem here, I am having trouble solving this problem. I keep getting the same error when i'm trying to create new registration ( http://localhost:3000/registrations/new?course_id=1 ) :
NoMethodError at /registrations
undefined method `id' for nil:NilClass
Here is my RegistrationsController:
class RegistrationsController < ApplicationController
before_action :set_registration, only: [:show, :edit, :update, :destroy]
def index
#registrations = Registration.all
end
def show
end
def new
#registration = Registration.new
#course = Course.new
#course = Course.find_by id: params["course_id"]
end
def create
#registration = Registration.new registration_params.merge(email: stripe_params["stripeEmail"], card_token: stripe_params["stripeToken"])
raise "Please Check Registration Errors" unless #registration.valid?
#registration.process_payment
#registration.save
redirect_to #registration, notice: 'Registration was successfully created.'
rescue Exception => e
flash[:error] = e.message
render :new
end
protect_from_forgery except: :webhook
def webhook
event = Stripe::Event.retrieve(params["id"])
case event.type
when "invoice.payment_succeeded" #renew subscription
Registration.find_by_customer_id(event.data.object.customer).renew
end
render status: :ok, json: "success"
end
private
def stripe_params
params.permit :stripeEmail, :stripeToken
end
def set_registration
#registration = Registration.find(params[:id])
end
def registration_params
params.require(:registration).permit(:course_id, :full_name, :company, :telephone, :email, :card_token)
end
end
My Registration Model:
class Registration < ActiveRecord::Base
belongs_to :course
def process_payment
customer_data = {email: email, card: card_token}.merge((course.plan.blank?)? {}: {plan: course.plan})
customer = Stripe::Customer.create customer_data
Stripe::Charge.create customer: customer.id,
amount: course.price * 100,
description: course.name,
currency: 'usd'
#Annotate Customer Id when Registration is Created
cusotmer_id = customer.id
end
def renew
update_attibute :end_date, Date.today + 1.month
end
end
Registration New.html.haml File :
%section#course-content
%section#ruby
%section.detailed-syllabus
.wrapper-inside
= form_for #registration, html: { class: "basic-grey" } do |f|
- if #registration.errors.any?
#error_explanation
%h2
= pluralize(#registration.errors.count, "error")
prohibited this registration from being saved:
%ul
- #registration.errors.full_messages.each do |message|
%li= message
.field
= f.hidden_field :course_id, value: #course.id
.field
= f.label :full_name
= f.text_field :full_name
.field
= f.label :company
= f.text_field :company
.field
= f.label :email
= f.text_field :email
.field
= f.label :telephone
= f.text_field :telephone
//‘Stripe.js’ will recognize the card data because we have marked the inputs with ‘data-stripe’ attribute as: number, cvv, exp-month and exp-year.
= javascript_include_tag "https://js.stripe.com/v2/"
:javascript
Stripe.setPublishableKey('#{Rails.application.secrets.stripe_publishable_key}');
= label_tag "Card Number", nil, required: true
.control-group
.controls
= text_field_tag :card_number, nil, class: "input-block-level", "data-stripe" => "number"
= label_tag "Card Verification", nil, required: true
.control-group
.controls
= text_field_tag :card_verification, nil, class: "input-block-level", "data-stripe" => "cvv"
= label_tag "Card Expires", nil, required: true
= select_tag :exp_month, options_for_select(Date::MONTHNAMES.compact.each_with_index.map { |name,i| ["#{i+1} - #{name}", i+1] }), include_blank: false, "data-stripe" => "exp-month", class: "span2"
= select_tag :exp_year, options_for_select((Date.today.year..(Date.today.year+10)).to_a), include_blank: false, "data-stripe" => "exp-year", class: "span1"
.actions
= f.submit "Registration Payment", class: "btn", style: "color: white;background: rgb(242, 118, 73);"
Does anyone know how to assist me in this? Greatly appreciate all the help.
Additional Can anyone please guide me through how to pass id between 2 models like this guy did between 2 models as he's creating a scaffold for one model but passing ID lets him create values for another model too without creating actions for another controller https://github.com/gotealeaf/stripe-basics.git
Edited:
GitHub Repository For This Code
https://github.com/ChiragArya/Stripe_CheckOut_Demo
From your comments, it appears the error is caused by :
#course.id being nil
The way to fix this is to ensure #course is defined properly. You need to do the following:
def new
#registration = Registration.new
#course = Course.find_by id: params["course_id"]
end
The other issue you have here is that your routes should be able to handle courses without having to append them with ?course_id=1:
#config/routes.rb
resources :registrations do
get :course_id, to: "registrations#new" #-> yoururl.com/registrations/:course_id
end
This will still give you the course_id param in the new action; just makes it more Rails.
--
Controller
You also need some structure in your code (you're aiming for fat model, thin controller). It looks like you're coming to Rails as a Ruby dev; you need to appreciate that Rails handles most of the exceptions etc for you.
Specifically, you need to look at how to remove code out of your actions:
def create
#registration = Registration.new registration_params
#registration.process_payment
if #registration.save
redirect_to #registration, notice: 'Registration was successfully created.'
else
# handle error here
end
end
private
def registration_params
params.require(:registration).permit(:course_id, :full_name, :company, :telephone, :email, :card_token).merge(email: stripe_params["stripeEmail"], card_token: stripe_params["stripeToken"])
end
-
`id' for nil:NilClass
Finally, you have to remember this error basically means the variable you're trying to invoke an action for is nil.
Ruby populates nil variables with a NilClass object, thus it's difficult to determine what the error actually is. All it means is that the variable you're trying to call a method on doesn't have the aforementioned method, as Ruby has populated it with the NilClass object.
Try changing Registration#new action to
def new
#course = Course.find(params[:course_id])
#registration = #course.registrations.new
end
add this in your def create
def create
#course = Course.find_by id: params["registration"]["course_id"]
#registration = Registration.new registration_params.merge(email: stripe_params["stripeEmail"], card_token: stripe_params["stripeToken"])
raise "Please Check Registration Errors" unless #registration.valid?
#registration.process_payment
#registration.save
redirect_to #registration, notice: 'Registration was successfully created.'
rescue Exception => e
flash[:error] = e.message
#course = Course.find_by id: params["registration"]["course_id"]
render :new
end
I've got a simple search form, in rails 4 app, that needs two params passed to be able to show relevant data.
I'm getting an 'Mysql2::Error: Unknown column 'data inputted' but the columns do exist. If I instead of '#search = Page.where(params[:one] && params[:two])' use '#search = Page.all' the data shows, but all of it shows.
Form
<%= form_tag(page_show_path, id: "search-form") do %>
<%= text_field_tag :one, params[:one], placeholder: "One" %>
<%= text_field_tag :two, params[:two], placeholder: "Two" %>
<%= submit_tag "Search", :name => nil %>
<% end %>
Model
def self.one(query)
where("one = ?", "%#{query}%")
end
def self.two(query)
where("two = ?", "%#{query}%")
end
Controller
def show
if (params[:one] && params[:two]).present?
#search = Page.where(params[:one] && params[:two])
else
redirect_to page_path, notice: "Not a valid combination"
end
end
You can Create and Use Scope.
scope :find_one_two, ->(query_one, query_two) { where("one = ? AND two = ? ", query_one, query_two) }
#search = Page.find_one_two(params[:one], params[:two])
OR
You can use.
#search = Page.where("one = ? AND two = ?", params[:one], params[:two])
def show
if (params[:one] && params[:two]).present?
#search = Page.where("one like ? AND two like ? ", "%#{params[:one]}%", "%#{params[:two]}%")
else
redirect_to page_path, notice: "Not a valid combination"
end
end
This may solve your problem.
I'm trying to set up the Mandrill API to send an email when a user clicks a button, but I cannot seem to get it to send. The email will send perfectly fine from the console, so I know it is not a problem with the template (Devise emails also send find).
I think it is to do with how I have set it up in the controller but I cannot find any help on where I should put it instead.
Here is the code from the controller:
def attending
#event = Event.find(params[:id])
type = params[:type]
if type == "attending" && #event.space != 0
current_user.attending << #event
#event.space = #event.space - 1
#event.save
AdminMailer.new_attending(#event.user, current_user)
redirect_to :back, notice: "You've joined the group. Your number will be sent to #{#event.user.name}"
else type == "unattending"
current_user.attending.delete(#event)
redirect_to :back, notice: "You've removed yourself from the group"
end
end
Here is the admin_mailer.rb
class AdminMailer < ActionMailer::Base
require 'mandrill'
def mandrill_client
#mandrill_client ||= Mandrill::API.new MANDRILL_API_KEY
end
def new_attending(creator, user)
template_name = "new-attending"
template_content = []
message = {
to: [{email: creator.email, name: creator.name}],
subject: "Someone wants to go riding with you!",
merge_vars: [
{rcpt: creator.email,
vars: [
{name: "CREATOR_NAME", content: creator.name},
{name: "USER_NAME", content: user.name},
{name: "USER_NUMBER", content: user.number}
]}
]
}
mandrill_client.messages.send_template template_name, template_content, message
end
end
And here is the link they click in the view.html.erb that should send the email:
<td><% if event.user != current_user && event.space != 0 && user_signed_in? %>
<% unless event.attendees.include?(current_user) %>
<%= link_to "Join", attending_event_path(event, type: "attending"), class: "btn btn-primary btn-xs", method: :put %>
<% end %>
<% end %></td>
Any help in figuring out why it's not sending would be great! As I said, it works in the console when I type:
AdminMailer.new_attending(#event, #user)
Please replace following code.
AdminMailer.new_attending(#event.user, current_user)
with
AdminMailer.new_attending(#event.user, current_user).deliver
I hope this will work.
I've build quite complex form which creates one prescription with many realtions. I am using this syntax in view:
- provide(:title, 'Create prescription')
%h1 Add medicines to prescription
.row
.span6.offset3
= form_for #prescription do |f|
= render 'shared/error_prescription_messages'
%p
= f.hidden_field :patient_id, :value => params[:patient_id]
= f.hidden_field :user_id, :value => current_user.id
= f.fields_for :relations do |builder|
= render 'child_form', :f => builder
%p= f.submit "Submit"
chlid_form is quite simple :
- it=f.options[:child_index].to_i
- n= it.to_s
%h2
= "Medicine ##{it+1}"
= f.hidden_field :medicine_id, :id => "my_medicine_id#{it}"
- if params[:prescription].nil? || params[:prescription][:relations_attributes][n.to_sym][:medicine_name].nil?
= f.autocomplete_field :medicine_name, autocomplete_medicine_name_relations_path, :id_element => "#my_medicine_id#{it}"
- else
= f.autocomplete_field :medicine_name, autocomplete_medicine_name_relations_path, :id_element => "#my_medicine_id#{it}", :value => params[:prescription][:relations_attributes][n.to_sym][:medicine_name]
= f.label :amount, "Amount of medicine boxes"
= f.number_field :amount, :value => 1
= f.label :daily
= f.number_field :daily, :value => 1
= f.label :period_in_days, "Duration of treatment (in days)"
= f.number_field :period_in_days, :value => 1
So as you can see I'm using f.options[:child_index] to get index of child (0,1,2...) cause I generate multiple items with this particular form. I then put it to variable it and sucessfully use it in :id_element => "#my_medicine_id#{it}" which works PERFECTLY fine (creates my_medicine_id0, my_medicine_id1 ....) Although it doesn't work in this line:
:value => params[:prescription][:relations_attributes][n.to_sym][:medicine_name]
where n is just n=it.to_s.
I though somethings wrong in controller but if I change this line to whatever
:value => params[:prescription][:relations_attributes]**[:'0']**[:medicine_name] or any other integer from 0 to 4 everything works great, but I NEED dynamic change in this one. So I got proof that it DOES work because it generates integer fine here "#my_medicine_id#{it}" but won't work in hash! And when I print the whole hash from params I get this:
{"patient_id"=>"7", "user_id"=>"1", "relations_attributes"=>{"0"=>{"medicine_id"=>"13490", "medicine_name"=>"Locacid 500 mcg/g (0,05%) (1 tuba 30 g)", "amount"=>"0", "daily"=>"1", "period_in_days"=>"1"}, "1"=>{"medicine_id"=>"", "medicine_name"=>"", "amount"=>"1", "daily"=>"1", "period_in_days"=>"1"}, "2"=>{"medicine_id"=>"", "medicine_name"=>"", "amount"=>"1", "daily"=>"1", "period_in_days"=>"1"}, "3"=>{"medicine_id"=>"", "medicine_name"=>"", "amount"=>"1", "daily"=>"1", "period_in_days"=>"1"}, "4"=>{"medicine_id"=>"", "medicine_name"=>"", "amount"=>"1", "daily"=>"1", "period_in_days"=>"1"}}}
so to get the values I need it's pretty obvious that
params[:prescription][:relations_attributes][SOME_KIND_OF_INETEGER][:medicine_name] should work, but doesn't.
Controller code:
class PrescriptionsController < ApplicationController
before_action :signed_in_user
before_action :doctor_user, only: [:new, :create]
before_action :pharmacist_user, only: [:update]
def new
#prescription =Prescription.new
5.times { #prescription.relations.build }
end
def create
#prescription = Prescription.new(new_prescription_params)
if #prescription.save
flash[:success] = "Prescription created."
redirect_to #prescription
else
5.times { #prescription.relations.build }
render 'new', :prescription => params[:prescription]
end
end
def show
#prescription = Prescription.find(params[:id])
#medicines = #prescription.medicines.paginate(page: params[:page], :per_page => 10)
end
def update
#prescription = Prescription.find(params[:id])
#patient = Patient.find(params[:patient_id])
if !prescription_expired?(#prescription)
#prescription.realized = 1
if #prescription.save
flash[:success] = "Prescription realized."
redirect_to #patient
else
redirect_to root_url
end
else
flash[:notice] = "Can't realize, prescription expired."
redirect_to #patient
end
end
private
def new_prescription_params
params.require(:prescription).
permit(:patient_id, :user_id, relations_attributes: [:medicine_id, :medicine_name, :amount, :daily, :period_in_days])
end
def doctor_user
redirect_to(root_url) unless current_user.function == "doctor"
end
def pharmacist_user
redirect_to(root_url) unless current_user.function == "pharmacist"
end
def prescription_expired?(presc)
presc.created_at < 1.month.ago
end
def signed_in_user
unless signed_in?
store_location
flash[:notice] = "Please log in."
redirect_to login_url
end
end
end
I run out of ideas so I ask you guys if anyone can help. Thanks.
There is no point in using params in your view since you already assigned those to your models. Also when you rendering your new action, those params doesn't exist as nothing has been send to the server yet. Just get rid of all the values from inputs.
Your partial should look like:
- it=f.options[:child_index].to_i
- n= it.to_s
%h2
= "Medicine ##{it+1}"
= f.hidden_field :medicine_id, :id => "my_medicine_id#{it}"
= f.autocomplete_field :medicine_name, autocomplete_medicine_name_relations_path
= f.label :amount, "Amount of medicine boxes"
= f.number_field :amount
= f.label :daily
= f.number_field :daily
= f.label :period_in_days, "Duration of treatment (in days)"
= f.number_field :period_in_days
If you want your fields to have default value, set default value inside your database.