top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Best practise for setting default URL parameters in the controller - Rails

+4 votes
460 views

I've been trying to set default URL parameters in the controller, which will also be used within the view. This is what I've got:

@params = params

defaults = { :date_from => '21/1/21014', :date_to => '21/2/21014', :data=>"Expense" }
if @params.any?
 @params = defaults.merge(@params)
else
 @params = defaults
end

.. seems messy, and doesn't work :( When params are present it still uses the default params. Not sure where it's going wrong and search on google yields many varieties. I know using default values in methods is something that I will repeat again so I was wondering what the best practice is for handling this.

posted Feb 2, 2014 by Deepti Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes

That is likely because it should be params not @params. I don't think you need the .any? test either, just let it merge the empty hash into defaults.

If you need to do this in more than one controller method then use a before filter.

answer Feb 2, 2014 by Seema Siddique
+1 vote

Personally, I'd say "best practice" is "don't do that" :-)

Instead of passing raw params to a view, build your model object (or a service object) with them and pass that in. And if you need defaults for missing attributes specified, the model/s.o. would be a better place for them.

answer Feb 2, 2014 by Kumar Mitrasen
Similar Questions
+2 votes

I have a rails application, in which I am using delayed_job_active_record gem for running background jobs. While using the method .delay with an object, I am getting the following mysql error:

"INCORRECT STRING VALUE: \'XE2X9CX93"X0A ...\' FOR COLUMN \'HANDLER\' AT ROW 1"

I already searched for the above error and found that its because of the difference in encoding in mysql and rails. The solution suggested by many programmers is to alter the encoding in mysql database to utf8. But I also read that MySQL's utf8 charset only partially implements proper UTF-8 encoding. It can only store UTF-8-encoded symbols that consist of one to three bytes; encoded symbols that take up four bytes aren't supported. Which might cause trouble in some other cases. Also, when I tried to insert the value directly in mysql, it worked like a charm. Suggesting that the issue might lie elsewhere. So, can anyone please suggest the right method to rectify this problem?

Any help would be greatly appreciated.

+1 vote

I was working on a test app and everything was going great. However now when I run "rails server" and none of the changes to the controller files are reflected.

If I make changes to HTML files, those are reflected. And yes, I'm sure am in the right directory editing the correct files.

I can get rid of actions in the controller files and it still works fine. Do I have to do something after I change the ruby files? I thought I could just save them then run 'rails server'... It had seemed to be working doing just that previously.

0 votes

I have built a rails application with jruby. Since I have no experience with deploying a rails application in a production environment, I have spent some time doing a manual deployment on a linux server with tomcat6 as application server.

In essence, I have made a war file of the rails application with help of the warbler gem and deployed that on the linux server. I managed to get it up and running, but not yet exactly as I need it to be.

I have still some configuration issues. I hope you can help me or point me to the right place, since it may be a tomcat question. But I assume there must be some rails/tomcat expertise among the members of this group...

The point is, the URL in the development server is localhost:3000/invoices (using rails server)
The URL in the tomcat production environment is localhost:8080/rails/invoices

I can't seem to figure out how I can change the port and the document root (/invoices instead of /rails/invoices) in tomcat.

Any suggestions?

+1 vote

I need current url in rails environment file. I can get current url in controller by the below command.
request.env[HTTP_HOST]

and now I want get current url in environment.rb file or intializer folder files

...