how to add a new tab in spree admin panel? - ruby-on-rails

I created a controller under Spree::Admin module and also I have mentioned in
routes.rb
match '/admin/new_tab => 'spree/admin/new_controller#index'
but this is working in front end.
How can I move this page to admin panel with new tab?

To make this controller work only if admin is signed in (like the other controllers in Admin namespace), it should inherit from Admin::BaseController.
To make the new tab, you should probably create Deface, something like this:
Deface::Override.new(:virtual_path => "spree/layouts/admin",
:name => "admin_content_admin_tab_parser",
:insert_bottom => "[data-hook='admin_tabs']",
:text => "<%= tab :new_tab, :url => 'admin/new_tab', :icon => 'icon-th-large' %>",
:disabled => false)

Related

Where should I create my rails view - Rails 4.0

I have a rails app whose homepage is as follows:
www.person.com
I then have a model - Person who has an id attribute.
If I want to create a view based on that ID, and I want the URL to look like this:
www.person.com/{Person.id}, or www.person.com/1
where should I create my view and what should I name it? Should it just be Views/Person/index.html.erb?
Rather, create show.html.erb in views/person(s)/
You also need to create a controller method in persons_controller.rb:
def show
# Insert options for person's page here
end
The magic happens in routes.rb, where you'll define the route for that show:
resources :persons, :path => '', :path_names => {:show => '/:id'}

Generate the URL for a generic path with possible options in Rails

I'm creating a customisable nav menu for our site and have run into the following problem.
I need to generate a URL to any controller and action on the site and optionally pass it parameters. I was able to do the former by simply saying:
url_for(:controller => nav[:controller_name], :action => nav[:action_name])
which is great for sending you to {controller}/{action}. eg. news/articles
Throwing options in suddenly changes the game. Now I need to send you to something like:
{controller}/{action}/{category}/{slug}/{id}
eg. news/articles/world-domination/montana-max-vows-revenge/12345
the helper for the above would be something along the lines of:
news_article_path('world-domination', 'montana-max-vows-revenge', '12345')
and I haven't been able to replicate that in a vanilla url_for due to the arguments.
What I have done, and I don't really like is:
url_for(send("#{nav[:controller_name]}_#{nav[:action_name]}_path", *nav[:options]))
which generates the helper using send and then passes it a kwargs list. I'm sure there's a better way to do that surely?
You can do this cleanly if you are able to name the options (split here over lines for legibility):
url_for({
:controller => nav[:controller_name],
:action => nav[:action_name]
}.merge(nav[:options] || {}))
where
nav = {
:controller_name => 'news',
:action_name => 'articles',
:options => {
:category => 'world-domination',
:slug => 'montana-max-vows-revenge',
:id => '12345'
}
}

Kaminari wrong url in the view

I've found similar questions about Kaminari, however none of the answers worked on my machine.
Basically, when I'm paginating, the number of pages is correct, but the URL I'm redirected to is wrong.
This one below is the action in my controller
def index_offered
#machines = Machine.not_sponsored.offered.order("created_at DESC").page(params[:page]).per(5)
end
Now, when I implement Kaminari in the view
<%= paginate #machines%>
I obtain 3 pages (correct) of pagination, the links are:
0.0.0.0:3000/?page=2
0.0.0.0:3000/?page=3
While they should be
0.0.0.0:3000/offered-machinery?page=2
0.0.0.0:3000/offered-machinery?page=3
Could the catch be nested in the routing? Here it is the route that regards the index_offered action
match 'offered-machinery' => 'machines#index_offered', :as => :offered_machinery
I've also tried to pass params in the view by typing
<%= paginate #machines , :params => {:controller => "Machines" , :action => 'index_offered'}%>
and uncommented the line in routes.rb:
match ':controller(/:action(/:id))(.:format)'
but in that case, I obtain these URLs instead:
0.0.0.0:3000/Machines/offered-machinery?page=2
0.0.0.0:3000/Machines/offered-machinery?page=3
How could I work around this?
Try to change de controller name to "machines":
{:controller => "machines" , :action => 'index_offered'}%>

Marking a string as HTML safe

I am trying to build my first Rails application and I'm using Ryan Heath's navigation_helper plugin to give me the current class in my navigation. I built my named routes as follows:
match 'games' => 'games#index', :as => :games
match 'new' => 'games#new', :as => :new
match 'previous' => 'games#previous', :as => :previous
match 'settings' => 'settings#index', :as => :settings
Then in my application_layout I added the following code
<%= navigation([:games, :new, :previous, :settings]).html_safe %>
From what I know of Rails the html_safe should force HTML to be rendered properly, but instead what I get is this:
<ul class="navigation">["<li class=\"current\"><a href=\"/games\">Games</a></li>", "<li class=\"\"><a href=\"/new\">New</a></li>", "<li class=\"\"><a href=\"/previous\">Previous</a></li>", "<li class=\"\"><a href=\"/settings\">Settings</a></li>"]</ul>
So am I doing something wrong or is the plugin doing something wrong? I know that the plugin was written back in 2.x days which from what I know handled HTML a bit differently, but I just don't know enough.
https://github.com/priceflex/navigation_helper/commit/ad7bf45db1845e9299e9da39cf214866b608dd47 try to use this fork wich fix issues for rails3
You can use raw() method to avoid escaping:
<%= raw(navigation([:games, :new, :previous, :settings])) %>

Rails 3: How to prevent users to change the database by typing a URL?

I have the following link:
link_to("Toggle", "/jobs/#{job.id}/toggle_is_money_paid", :remote => true)
which toggles the is_money_paid field of a job using an Ajax request:
def toggle_is_money_paid
job = Job.find(params[:id])
job.update_attributes(:is_money_paid => !job.is_money_paid)
render :nothing => true
end
# config/routes.rb
match "/jobs/:id/toggle_is_money_paid" => "jobs#toggle_is_money_paid"
However, if user types directly:
http://localhost:3001/jobs/200/toggle_is_money_paid
in the browser, it will toggle the is_money_paid field of job #200.
How could I prevent this, such that users could toggle the field only by pressing the link.
You could prevent this by not defining the route using match, but instead by using one of the HTTP verbs that isn't get. More than likely, you'll want to use put:
put "/jobs/:id/toggle_is_money_paid" => "jobs#toggle_is_money_paid"
Then you'll change your link_to to this:
link_to("Toggle", "/jobs/#{job.id}/toggle_is_money_paid", :remote => true, :method => :put)

Resources