how to select database value based on most common field using rails - ruby-on-rails

def selected
#enquiries = Enquiry.all
#enquiry_details = EnquiryDetail.all
#comments = Comment.all
#enquiries.where("created_at >= ? AND created_at <= ?", Time.zone.now.beginning_of_day, Time.zone.now.end_of_day)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #enquiries and #enquiry_details and #comments }
end
end
How to call this method in my view.
<%= link_to '<button type="button">SELECT</button>'.html_safe, method: :selected %>

Create a route for the action selected and then do this;
<%= button_to "SELECT", { :controller => "CONTROLLER", :action => "selected"} %>

Related

Rails - Update attributes with best in place gem

Using best in place to update a attribute in a Order form rails spit out undefined methodupdate_attributes' for #`
the form is not on a update action but in a sold action, so the form must be inside of a update action?
<%= best_in_place order, :track_number, :as => :input, :nil => "Track code", :url => sold_order_path(order)%>
Mark as:
<%= link_to "Sent", sent_order_path(order), :class => 'btn btn_small' %>
def sold
#q = Order.where('seller_id = ? and status = ?', current_vitrine.id, params[:status] || Order.statuses[0]).ransack(params[:q])
#orders = #q.result(distinct: true).paginate(page: params[:page], per_page: 22)
#order = Order.find_by_id(params[:id])
respond_to do |format|
if Order.where('seller_id = ? and status = ?', current_vitrine.id, params[:status] || Order.statuses[0]).update_attributes(params[:order])
format.html { redirect_to :back, notice: 'Comment was successfully updated.' }
format.json { respond_with_bip(#order) }
else
format.html { render action: 'sold' }
format.json { respond_with_bip(#order) }
end
end
end

rails3 update param in controller

I want to update 2 column values in a Rails3 controller if a certain submit button is used on the form.
Submit button on form:
<%= simple_form_for #costproject, :html => {:class => 'form-horizontal'}, :validate => true do |f| %>
...
<%= f.submit 'Submit to Division', :class => 'btn btn-warning', :name => "submit1" %>
Update logic in Controller:
class CostprojectsController < ApplicationController
...
def update
...
params[:coststatus_id] = 2 if params[:submit1]
params[:submit_date] = Date.today if params[:submit1]
respond_to do |format|
if #costproject.update_attributes(params[:costproject])
flash[:success] = "Project Submitted" if #costproject.previous_changes.include?(:submit_date)
format.html { redirect_to nextpath }
format.json { render json: #costproject }
else
format.html { render action: "edit" }
format.json { render json: #costproject.errors, status: :unprocessable_entity }
end
end
end
If I stop execution with an alert, it looks like the params have been set. But, the values don't get saved to the db.
You need to update the model if you want it save to the DB. You're just changing params in your update action.
You need something like this:
def update
#cost_project = Costproject.find(params[:id])
#cost_project.update(coststatus_id: 2, submit_date: Date.today) if params[:submit1]
end
the .update will update and save the record in the DB
This seems to work:
params[:costproject][:coststatus_id] = 2 if params[:submit1]
params[:costproject][:submit_date] = Date.today if params[:submit1]
respond_to do |format|

Rails Ajax render partial with local variable

Hey I am trying to render a partial using ajax everything work in rails normal form but when I am rendering a partial using ajax local variables don't work I am trying to create a like and dislike system for that I have created a model called FeedLike here I can like and dislike using simple create and destroy but when I am using ajax and there I am calling this action below :
$('#feed_like').html("<%= j render :partial => 'shared/dislike', :locals => { feed: #feed= #feed} %>");
I am getting the error No route matches
{:action=>"destroy", :controller=>"feed_likes", :feed_id=>nil, :user_id=>1}
And my dislike link is like this it is a partial which is under :
<div id="feed_dislike">
<%= link_to "Dislike",{ :action => 'destroy', :controller => 'feed_likes', :feed_id => #feed, :user_id => current_user.id }, class: "btn btn-primary" %>
</div>
everything work fine when I am not using ajax but when I am rendering partial why #feed is not getting any value what am I supposed to do . To get the value of feed.id . feed.id is coming from and model called Feed and it is under look and there I am rendering these two forms using partials .
now I am pasting some codes which will give you what I am doing :
#shared/_feed.html.erb
<% if #feed_items.try(:any?) %>
<ol class="microposts">
<%= render #feed_items %>
</ol>
<%= will_paginate #feed_items %>
<% end %>
And #feed_items is coming from :
#feeds/_feed.html.erb
<li id="feed-<%= feed.id %>">
<%= image_tag(current_user.avatar.url(:thumb), :size => '50x50') %>
<span class="user"><%= link_to feed.user.name, feed.user %></span>
<span class="content"><%= feed.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(feed.created_at) %> ago.
</span>
<% #like =FeedLike.where(:user_id => "#{current_user.id}", :feed_id => "#{feed.id}")
%>
<%= render :partial => 'shared/feed_like', locals: { feed: #feed= feed.id} %>
</li>
And now feed_like partial :
#shared/_feed_like.html.erb
<% if #like.count == 0 %>
<%= render :partial => 'shared/like', locals: { feed1: #feed= #feed} %>
<% else %>
<%= render :partial => 'shared/dislike', locals: { feed: #feed} %>
<% end %>
Controllers code for :
class FeedsController < ApplicationController
before_action :authenticate_user! ,only: [:create, :destroy]
def create
#feed = current_user.feeds.build(feed_params)
#feed_likes = FeedLike.new
if #feed.save
#redirect_to root_url
respond_to do |format|
format.html { redirect_to root_url }
format.js
end
else
respond_to do |format|
format.html { redirect_to root_url }
format.js { render :js=>'alert("you can not leave post empty");' }
end
end
end
def destroy
end
private
def feed_params
params.require(:feed).permit(:content)
end
end
FeedsLike controller :
class FeedLikesController < ApplicationController
before_action :authenticate_user! ,only: [:create, :destroy]
def index
#fees = FeedLike.all
respond_to do |format|
format.html
format.js
end
end
def update
#feed_likes = FeedLike.find_or_create_by(feed_like_params)
respond_to do |format|
if #feed_likes.save
format.html { redirect_to root_url, notice: 'Like ' }
else
end
end
end
def create
#feed_likes = FeedLike.find_or_create_by(:feed_id => params[:feed_id],:user_id =>params[:user_id])
respond_to do |format|
if #feed_likes.save
format.html { redirect_to root_url, notice: 'Like ' }
format.js
else
end
end
end
def delete
end
def destroy
#feed_likes = FeedLike.where(:feed_id => params[:feed_id],:user_id =>params[:user_id])
respond_to do |format|
if #feed_likes.destroy_all
format.html { redirect_to root_url, notice: 'Unlike ' }
else
end
end
end
def feed_like_params
params.require(:feed_like).permit(:user_id, :feed_id)
#params[:market_place]
end
end
And staticpage controller :
class StaticPagesController < ApplicationController
before_action :authenticate_user!
def home
if user_signed_in?
#feed_likes = current_user.feed_likes.new
#feed = current_user.feeds.build
#feed_items = current_user.feed.paginate(page: params[:page])
# #likes = Feed.find(:all,
# :select => '"feeds".id, "feeds".content, count("feed_likes".id) as Like',
# :from => :feeds,
# :left_join => '"feed_likes"',
# :on => '"feeds".id = "feed_likes"."feed".id',
# :limit => 5
# )
# #like =Feed.find_by_sql "
# SELECT id
# FROM feed_likes
# WHERE user_id = #{current_user.id}
# AND feed_id =#{feed.id}
# LIMIT 0 , 30
# "
end
end
def help
end
def about
end
def contact
end
end
your feed is nil which is why it is crashing
please add this in your feed_likes controller.
before_action :get_feed ,only: [:create, :destroy]
and at the bottom add this method
def get_feed
#feed= Feed.find(params[:feed_id])
end
Your feed definition looks wrong in your javascript file. Try this instead:
$('#feed_like').html("<%= j render :partial => 'shared/dislike', :locals => { feed: #feed} %>");

Want to have a partial update with ajax

I have a form:
<% form_tag({:controller => "/faq", :action => 'search_ajax'}, :update => "help_results", remote: true) do %>
that is going to the search_ajax action which is supposed to update the help_results div. That action is supposed to render a partial at the end, but I for sure am having syntax issues:
def search_ajax
#categories = HelpCategory.find(:all)
if params[:query].not_blank? && params[:query] != "Search for help about..."
#query = params[:query]
#terms = params[:query].split.map {|t| "%#{t.downcase}%" }
options = {
:allow_partial_match => true,
:order => 'fulltext_match_score DESC',
}
#results = SearchableText.search_model(HelpItem, #query, options).uniq
#results = {"Search Results" => #results} if !#results.blank?
#complicated_query = HelpItem.is_complicated_query?(#query)
#search_included = true
else
#results = HelpItem.all.group_by {|item| item.category.name rescue "Misc"}
#search_included = false
end
render :partial => "results"
respond_to do |format|
format.js do
render :partial => 'results'
end
format.html do
render :partial => 'results'
end
end
end
Some of the respond_to area is commented out. I am getting the error message:
ActionController::DoubleRenderError in FaqController#search_ajax
I know that if I add the remote => true helper into my form that the controller action needs to respond to js. I'm pretty sure this is a syntax error. The respond_to also comes at the end of my controller action.
Remove the
render :partial => "results"
Above the respond_to block

rails render is not working in create action

In my controller I coded like this
class CompanyUploadRequestsController < ApiController
def index
respond_to do |format|
format.html { render action: "index" }
end
end
def create
puts params
respond_to do |format|
format.html { render action: "index" }
end
end
def new
end
end
and in my view new.html.haml file
- page_title("Upload Company")
%h1 Upload Company
%hr
#upload-form
= simple_form_for(#company_upload, :as => :company_update, :url => company_upload_requests_path(#company_upload), :html => { :class => 'file-style'}) do |f|
= f.error_notification
.form-inputs
= f.input :requestname, :label => false, :id => "request_name_input"
= f.input :file,:as => :file, :label => false, :id => "file_select_input"
.form-actions
!= link_to 'Cancel', company_upload_requests_path, :class => 'btn'
= f.button :submit, 'Upload', :class => 'btn-primary'
In my index.html.haml file I have this
- page_title("Upload Company")
%h1 Company index
= link_to("Upload File", new_company_upload_request_path, :class => "btn btn-primary pull-right")
The problem is when I click upload button its not render to index page from create
Here I got the log like this
Processing by CompanyUploadRequestsController#create as HTML
Parameters: {"utf8"=>"?", "authenticity_token"=>"oygIP62E4GHefhN9OnvB3sKhddIb4CN/izfvF5GQtuI=", "company_update"=>{"requestname"=>""}, "commit"=>"Upload"}
Rendered company_upload_requests/create.html.haml within layouts/application (9.8ms)
How can I render to index action and view index file content.
Use like this.
def index
#company_uploads = ModelName.all
respond_to do |format|
format.html
end
end
No need to render index action in index response.
def create
puts params
respond_to do |format|
format.html { render "index" }
end
end
Change the render in your create method to redirect_to
def create
puts params
respond_to do |format|
format.html { redirect_to action: "index" }
end
end
For more explanation on render vs redirect_to, see this SO Question or this.

Resources