I have a screen where a user can select differente kinds of plans for his account. Like this:
#plans
<% Plan.all.order(:amount).each do |plan| %>
<%= render 'shared/plan_detail', {plan: plan, button_text: 'Choose this' } %>
<% end %>
#plan_detail
<div class="plan-details">
<header class= <%= plan.css %> >
<h3><%= plan.name %></h3>
<small><%= plan.visibility %></small>
<p><%= plan.card_description.html_safe %></p>
<span class="plan-price"><sup>$</sup><%= plan.amount.to_s %></span>
</header>
<article>
<%= plan.features_description.html_safe %>
<%= link_to button_text, {:controller => "accounts", :action => "update_plan", :plan => plan.id }, title: button_text, :class=>"btn btn-md btn-block btn-outline-green dialog-open" %>
</article>
</div><!-- end plan details -->
And In my controller i have:
#accounts_controller.rb
def update_plan
#plan = Plan.find(params[:plan])
current_user.plan = #plan
current_user.save
end
My routes its like this
get '/account/plans', to: 'accounts#plans', as: :update_plan
put '/account/plans', to: 'accounts#update_plan'
But I click on the button, and nothing happens. What Im doing wrong here?
This is a long shot, but seeing that your link has dialog-open I wouldn't be surprised if there was some Javascript preventing your link from working. In order to debug this further I would
a) Check browser's Javascript console for any errors
b) Remove the dialog-open class to see what happens
Related
controller:
def add_changelog
changlog_alert= params[:program][:changlog_alert]
#program.update(changlog_alert: changlog_alert)
flash[:notice] = "Changelog Updated Successfully."
redirect_to portal_admins_changelog_settings_path(#enterprise, #program)
end
routes:
namespace :admins do
get "changelog_settings"
post "add_changelog"
end
changelog_settings.html.erb
<div class="large-12 small-12 columns settings_menu_content px-6 white_shadow p-5">
<%= render partial: "portal/admins/dashboard_settings/changelog" %>
</div>
_changelog.html.erb
<%= form_for #program, url: portal_admins_add_changelog_path(#enterprise, #program), method: :post do |f| %>
<%= f.text_area :changelog_alert, value: #program.changelog_alert, rows: "3" %>
<div style="text-align: right;">
<p id="limit" style="text-align: right;margin-top: -10px;color: gray;"> <%= #program.changelog_alert ? (300 - #program.changelog_alert.split(" ").count) : '300' %> Words Remaining </p>
<%= f.submit "Save Changes", class: "btn btn-blue" %>
</div>
<% end %>
Rake route
portal_admins_changelog_settings GET /app/:enterprise_id/:program_id/admins
changelog_settings(.:format) portal/admins#changelog_settings {:subdomain=>"portal"}
So mainly this _changelog.html.erb file trying to show and seems like route also fine but everytime it goes to different page
I have Challenges containing Puns, and it is possible to vote on puns. On the Challenge Show page, all puns are rendered and show their votes count. This is currently on the view page:
<%= render #challenge.puns.reverse %>
<br>
<div id="form">
<%= render "puns/form" %>
</div>
I want the puns form to appear above the items (puns) already submitted. But if swap them around, like this:
<div id="form">
<%= render "puns/form" %>
</div>
<%= render #challenge.puns.reverse %>
I get a controller error and pun.id is not suddenly not available and the voting link breaks.
No route matches {:action=>"upvote", :challenge_id=>"9", :controller=>"puns", :id=>nil}, missing required keys: [:id]
Here is the puns/form part that is causing the issue
<% if signed_in? %>
<% if current_user.voted_for? pun %>
<%= pun.votes_for.size %>
<span class="pun_text"><%= link_to pun.pun_text, challenge_pun_path(#challenge, pun.id) %></span>
<% else %>
<%= link_to like_challenge_pun_path(#challenge, pun.id), method: :put do %>
<span class="heart_like">❤</span> <%= pun.votes_for.size %>
<% end %>
<span class="pun_text"><%= link_to pun.pun_text, challenge_pun_path(#challenge, pun.id) %></span>
<% end %>
<% end %>
It is the like_challenge_pun_path that throws an error but I cannot understand why. I am declaring #challenge again here, so it should be able to get the id.
Here is the form for the puns:
<%= form_for([#challenge, #challenge.puns.build]) do |f| %>
<span class=".emoji-picker-container">
<%= f.text_area :pun_text, placeholder: "Add pun", data: { emojiable: true } %>
</span>
<%= f.submit %>
<% end %>
Also, here is my routes setup
resources :challenges do
resources :puns do
member do
put "like", to: "puns#upvote"
put "dislike", to: "puns#downvote"
end
end
end
and the corresponding action to upvote
def upvote
#pun = #challenge.puns.find(params[:id])
#pun.upvote_by current_user
redirect_to #challenge
end
Can anyone help?
I think the code is for the puns collection.
I assume the issue is that in the form you have something like:
#challenge.puns.build
So in #challenge.puns collection appears not persisted record (without id), so path for this model cannot be generated.
As a quick solution I suggest:
<%= render #challenge.puns.reverse.select(&:persisted?) %>
UPDATE:
As I assumed you have
<%= form_for([#challenge, #challenge.puns.build]) do |f| %>
You can also try:
<%= form_for([#challenge, Pun.new]) do |f| %>
Or solve it in the controller. But need to see controller code for it.
I have a Rails 3.2.22 app that tracks dispatch calls and each call belongs to a region and a region has many calls. In my views I originally had all calls from all regions, but now I'm filtering by regions using a simple form_tag in the view and passing the region ID as a param to the controller and back to the view.
So locally if I hit the calls index view I will trigger a url like:
http://loopify.xyz:9001/calls?utf8=%E2%9C%93®ion=1
Which will then in my views show me all calls with the region id of "1". I can switch to different regions in the views and it will display the proper filtered calls by region.
When I deployed this to my staging server, the params filtering does not work at all and shows all calls even though when I select a region I will get a URL like:
http://staging.example.com/calls?utf8=%E2%9C%93®ion=1
Here is the index action of my calls controller:
def index
if params[:region].present?
#region = params[:region]
#assigned = Call.includes(:units, :transferred_from, :transferred_to, :nature, :region, :service_level).where(region_id: params[:region]).assigned_calls.until_end_of_day
#unassigned = Call.includes(:units, :transferred_from, :transferred_to, :nature, :region, :service_level).where(region_id: params[:region]).unassigned_calls.until_end_of_day
else
params[:region] = "1"
#assigned = Call.includes(:units, :transferred_from, :transferred_to, :nature, :region, :service_level).where(region_id: params[:region]).assigned_calls.until_end_of_day
#unassigned = Call.includes(:units, :transferred_from, :transferred_to, :nature, :region, :service_level).where(region_id: params[:region]).unassigned_calls.until_end_of_day
end
#note = Note.new
#units = Unit.active.order("unit_name").map{|unit| unit.calls.where(call_status: "open").empty? ? ["#{unit.unit_name} #{unit.unit_type.unit_type} #{unit.status.unit_status}", unit.id] : ["#{unit.unit_name} (on call) #{unit.unit_type.unit_type} #{unit.status.unit_status}", unit.id] }
end
Here is my index.html.erb view:
<div id="active">
<%= render "assigned_calls" %>
</div>
<div id="inactive">
<%= render "unassigned_calls" %>
</div>
<script>
$(function() {
setInterval(function(){
$.getScript('/calls/?region=<%= params[:region] %>')
}, 20000);
});
</script>
Here is the index.js.erb file to allow ajax refresh and JS
$("#active").html("<%= escape_javascript render("assigned_calls") %>");
$("#inactive").html("<%= escape_javascript render("unassigned_calls") %>");
Here is an excerpt of the assigned partial. Both assigned and unassigned really large to display here in their entirety but will be happy to supply them in a github gist if you need more context.
<div class="page-header well">
<h3><%= pluralize(#assigned.size, "Active Call") %></h3>
</div>
<%= form_tag calls_path, :method => 'get' do %>
<%= select_tag "region", options_from_collection_for_select(Region.order(:area), :id, :area, selected: #region), prompt: "Choose Region" %>
<%= submit_tag "Select", :name => nil, :class => 'btn' %>
<% end %>
<% #assigned.each_with_index do |call, index| %>
<div class="widget">
<div class="widget-header">
<div class="pull-right">
<%= link_to 'View', call, :class => 'btn btn-primary btn-small'%>
<% if dispatch? || admin? || manager? || operations? %>
<%= link_to 'Edit', edit_call_path(call), :class => 'btn btn-info btn-small'%>
<%= link_to "Close", '#close-modal', data: {toggle: "modal", target: "#close-modal#{index}" }, class: 'btn btn-small btn-warning' %>
<%= render 'layouts/close_call_modal', call: call, index: index %>
<%= link_to "Cancel", '#cancel-modal', data: {toggle: "modal", target: "#cancel-modal#{index}" }, class: 'btn btn-small btn-danger' %>
<%= render 'layouts/cancel_call_modal', call: call, index: index %>
<%= link_to "Notes", '#note-modal', data: {toggle: "modal", target: "#note-modal#{index}" }, class: 'btn btn-small btn-primary' %>
<%= render 'layouts/note_modal', call: call, index: index %>
<% end %>
</div>
<i class="icon-phone"></i>
<h3><%= link_to call.incident_number, call %> <span><%= status(call) %></span></h3>
<% if call.wait_return == "yes" && call.parent_call_id == nil %>
<i class="icon-arrow-right dim"></i> <span class="badge badge-important" >Initial Transport</span>
<% end %>
<% if call.wait_return == "yes" && call.parent_call_id.present? %>
<i class="icon-arrow-left dim"></i> <span class="badge badge-important" >Return Transport</span>
<% end %>
<% if call.traffic_type == "Emergency" %>
<span class="badge badge-important"><%= call.traffic_type %></span>
<% else %>
<span class="badge badge-info"><%= call.traffic_type %></span>
<% end %>
<span class="badge badge-primary" ><%= call.region.try(:area) %></span>
<span class="badge badge-info" ><%= call.service_level.try(:level_of_service) %></span>
</div>
<% end %>
I'm really not sure what the problem is here. In development I'm serving the app with Thin, in staging I'm using passenger and nginx.
Is there something in my code that will not work in a production environment that I'm missing?
To recap, the region filtering works perfectly in development but once deployed to staging the filtering does not work.
I dug into the logs and here is the request from both dev (local) and prod (staging) logs:
dev:
Started GET "/calls/?region=1&_=1468278029235" for 127.0.0.1 at 2016-07-11 18:00:29 -0500
Processing by CallsController#index as JS
prod:
Started GET "/calls/?region=1&_=1468278074295" for 192.168.130.1 at 2016-07-11 18:01:14 -0500
Processing by CallsController#index as JS
I'm not sure why this even happens/works but it does. If I turn the log level in production.rb from info to debug the param filtering works fine. I saw a reference to this on an old github issue. Rails core closed it as a non-bug, but obviously there's something to this. Unfortunately 3.2 is not supported anymore so this "hack" will have to do. Now it's time to upgrade the app to 4.2.6.
I have an application with a list of majors and each one is tagged with categories using the acts-as-taggable-on gem. I have a page where you can explore majors by category. So, you see the categories and grouped under the category is a list of the majors.
My categories_controller.rb file:
def index
#creative = Major.tagged_with("creative arts")
#engineering = Major.tagged_with("engineering and technology")
#mechanics = Major.tagged_with("mechanics and construction")
#transportation = Major.tagged_with("transportation")
#science = Major.tagged_with("science")
#math = Major.tagged_with("math")
#resources = Major.tagged_with("natural resources")
#healthcare = Major.tagged_with("health care")
#social_sciences = Major.tagged_with("social sciences")
#education = Major.tagged_with("education")
#law = Major.tagged_with("law")
#management = Major.tagged_with("management and marketing")
#administrative = Major.tagged_with("administrative and clerical")
#services = Major.tagged_with("services")
#tags = Major.tag_counts
end
You can see the duplication. This is compounded on the view template.
Here's part of the index.html.erb page:
<!-- Creative Arts -->
<h2 class="major-categories-landing">Creative Arts</h2>
<% #creative.sample(10).each do |creative| %>
<%= link_to creative, class: 'span2 category-landing' do %>
<%= image_tag creative.image(:similar), class: 'img-polaroid', id: 'category-landing-list' %>
<p class="category-landing-list-name"><%= creative.name %></p>
<% end %>
<% end %>
<%= link_to "View all #{#creative.count} majors in this category.", category_path("creative arts"), class: "view-category-show-page" %>
<!-- Social Sciences -->
<h2 class="major-categories-landing">Social Sciences</h2>
<% #social_sciences.sample(10).each do |ss| %>
<%= link_to ss, class: 'span2 category-landing' do %>
<%= image_tag ss.image(:similar), class: 'img-polaroid', id: 'category-landing-list' %>
<p class="category-landing-list-name"><%= ss.name %></p>
<% end %>
<% end %>
<%= link_to "View all #{#social_sciences.count} majors in this category.", category_path("social sciences"), class: "view-category-show-page" %>
and so on for each category. I have tried #category = Major.tagged_with(params[:tag]) and many variations to that to no avail. This is my first time working with acts_as_taggable_on and although I've read the documentation over and over I can't quite figure this out.
I hope to extend this out throughout the application and so I want to figure it out now before I get a lot duplicate code. Thanks for sharing any ideas or suggestions!!
I am running a rails 3.2.11 app.
UPDATE
Here's how much better this is now:
My categories_controller.rb file:
def index
#major_categories = ["creative arts", "social sciences", "science", ....]
end
My index.html.erb page:
<% #major_categories.each do |c| %>
<!-- Title and blue strip -->
<div class="category-strip">
<div class="container">
<h2 class="major-categories-landing"><%= c %></h2>
</div>
</div>
<!-- Show Each Major in this Category -->
<div class="container">
<div class="row-fluid">
<% Major.tagged_with(c).order('RANDOM()').limit(10).each do |major| %>
<%= link_to major, class: 'span2 category-landing' do %>
<%= image_tag major.image(:similar), class: 'img-polaroid' %>
<p class="category-landing-list-name"><%= major.name %></p>
<% end %>
<% end %>
</div>
<!-- Link to View All Majors -->
<div class="row-fluid">
<div class="view-all-category">
<%= link_to "View all #{Major.tagged_with(c).count} majors in this category.", category_path(c), class: "view-category-show-page" %>
</div>
</div>
</div>
<% end %>
I would do something like this:
# in categories_controller.rb
def index
#categories = ["creative arts", "engineering and technology",
"mechanics and construction", ...]
end
# in index.html.erb
<%= render partial: "category", collection: #categories %>
# in _category.html.erb
<h2 class="major-categories-landing"><%= category.titleize %></h2>
<% Major.tagged_with(category).order('rand()').limit(10).each do |major| %>
<%= link_to major, class: 'span2 category-landing' do %>
<%= image_tag major.image(:similar), class: 'img-polaroid',
id: 'category-landing-list' %>
<p class="category-landing-list-name"><%= major.name %></p>
<% end %>
<% end %>
<%= link_to "View all #{Major.tagged_with(category).count} majors in this category.",
category_path(category), class: "view-category-show-page" %>
Btw: The link to each major is invalid html. A link (because a it is an inline element) should not contain a paragraph (because p is a box element). Furthermore each link for each category would have the same id, but ids must be unique in each html document.
Just trying to get my head around the following, probably basic I know. I am looping through an array of records using .each and would like to view the post i click via an ajax request on the same page
<h2>Recent News</h2>
<ul>
<% #tynewyddpost.reverse.each do |t| %>
<li>
<% single = t.photos.first %>
<a class="photo" href="#"><%= image_tag(single.avatar.url(:thumbnail_news_images)) %></a>
<p><%= link_to t.title, tynewyddnews_path(:type => 'tynewyddnews'), :remote => true %></p>
<p class="date"><%= date_output(t.published_on) %></p>
</li>
<% end %>
</ul>
So when i click the title it will render the same post no matter which record i click.
The partial i render
<div class="post-item">
<% #tynewyddpost.reverse.each do |t| %>
<h2><%= t.title %></h2>
<div id="work-samples">
<% for photo in t.photos %>
<%= image_tag(photo.avatar.url(:news_images), :class => 'work-sample') %>
<% end %>
</div>
<p class="post-description"><%= t.comments.html_safe %></p>
<div class="post-item-panel">
<ul>
<li class="date">
<p><%= date_output(t.published_on) %></p>
</li>
</ul>
</div>
</div>
<% end %>
Controller
def tynewyddnews
#title = 'Ty Newydd News'
tynewyddpost = Post.tynewydd_posts.reverse
tynewyddpost.pop
#tynewyddpost = tynewyddpost
#tynewyddpostlatest = Post.tynewydd_posts.first
end
Scope
scope :tynewydd_posts, :include => :department, :conditions => {"departments.name" => "Ty Newydd"}, :order => "posts.published_on DESC"
My question is how to get the particular post i have clicked. I cant do
<%= #tynewyydpost.title %>
As i get undefined method title for array. Bit of theory here i know but how to get an individual record from an array in this instance
Any help appreciated
You need to pass the id of the post you're clicked on:
<p><%= link_to t.title, tynewyddnews_path(:type => 'tynewyddnews', :post_id => t.id), :remote => true %></p>
so in your controller you can do
#theposticlickedon = Post.find(params[:post_id])
or
#theposticlickedon = Post.tynewydd_posts.find(params[:post_id])
However, you also may want to define a different path to show the individual post, instead of the tynewyddnews_path you have in your link.
You need to specify in every link ID of this post.
For example:
<%= link_to t.title, tynewyddnews_path(:type => 'tynewyddnews'), :post_id=>t.id, :remote => true %>
And than specify that in controller action you're calling , by finding this by id
#tynewyddnews=Post.find(params[:post_id])
Than you're partial instance #tynewyddnews will be clicked post